session_start()无法正常工作

时间:2016-03-17 20:03:25

标签: php html

我希望你做得很好, 我显然是使用登录帐户创建这个网站,我已经在本地数据库和服务器数据库中注册了我的用户。我的HTML / PHP代码在运行时没有显示任何错误。我检查了我的数据库连接。这是正确的。该网站允许我使用任何用户和密码登录。它似乎没有正确验证我输入的数据。虽然我检查了我的SQL命令。 我想知道你是否可以帮助我这些家伙。你是最棒的! :) 谢谢你,提前谢谢 这是我的代码中有用的部分: 我的标题 - Header.php:

<html>

<?php
/* static $called = FALSE;
if (!$called)
{*/
session_start();
/*$called = true;
}*/

include_once 'debugging.php';
?>
<head>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div >
        <dt id="navbar1" class ="navbar">
        <a href="Index.php" class="followingLink">Home</a>
        <a href="Upload.php">Upload Videos</a>
        </dt>
    </div>
    <?php
    if (isset($_SESSION['logged'])) { 
    echo '<div class="right navbar" id = "navbar2">
    <a href="Index.php" class = "right followingLink1">Log out</a>                
    <p class = "right">/</p>
        <a href="Edit_Account.php" class = "right">Edit Account</a>
    <img src="http://www.extremetech.com/wp-content/uploads/2013/11/emp-blast.jpg?type=square"
    height="42" width="42" class = "right"/>
    </div>';
     } else {
      echo '<div class="right navbar" id = "navbar2">
      <a href="Login.php" class = "right">Login</a>
      <p class = "right">/</p>
      <a href="Sign_Up.php" class = "right">Sign Up</a>
      </div>';
      } 
    ?>

进展 - 反馈:

好吧,伙计们,我试过你告诉我要做的事。它开始让我自动登录。可能会话['logged']变量声明被认为是真的。只有当用户从登录页面登录时才将其设置为true。但它没有以这种方式运作。 这是我的登录页面代码:

<?php
    include_once 'Header.php';
    ?>

        <div id="container">
            <br>


            <?php
            /*
              if($_DEBUG)
              {
              ini_set('display_errors', 1);
              ini_set('log_errors', 1);
              ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
              error_reporting(E_ALL);
              }


              $page_title = 'Login';/* */

    //in this page we do things slightly differently - the code for validation    
    and displaying messages is done
    //before we display the form 
            echo '<div id = "div_1"><h1>Login</h1>';

    //display the form
            echo '<div id="div_2"><div id="div_2">
       <form action="index.php" method="post">
           <label>UserName<br>
           <span class="small">enter your username</span>
           </label>
           <input type="text" name="UserName" value=""/>

           <label><br>Password<br>
           <span class="small">enter your password</span>
           </label>
           <input type="password" name="Password" />

           <button type="submit" name="submit" value="Login" />Log in</button>
           <input type ="hidden" name="submitted" value="TRUE">
         </form>
         </div>
    </div>';

             if (isset($_POST['submitted'])) {
              //require_once is similar to 'include' but ensures the code is not 
    copied multiple times  
              require_once('LoginFunctions.php');

              //list() is a way of assigning multiple values at the same time
              //checkLogin() function returns an array so list here assigns the 
    values in the array to $check and $data
              list($check, $data) = checkLogin($_POST['UserName'], 
    $_POST['Password']);

              if ($check) {
              setcookie('FName', $data['FName'], time()+ 900 ) ;  //cookie  
    expires after 15 mins
              setcookie('LName', $data['LName'], time() + 900 ) ;
              //
              //use session variables instead of cookies
              //these variables should now be available to all pages in the 
    application as long as the users session exists
              $_SESSION['FName'] = $data['FName'];
              $_SESSION['LName'] = $data['LName'];
              $_SESSION['UserName'] = $data['UserName'];
              //to enable $_SESSION array to be populated we always need to call 
    start_session() - this is done in header.php
              //print_r is will print out the contents of an array
              print_r($_SESSION);
              //
              //Redirect to another page
              $url = absolute_url('Index.php');  //function defined in 
    Loginfunctions.php to give absolute path for required page
              $_SESSION['logged'] = TRUE;
              //this version of the header function is used to redirect to 
    another page
              header("Location: $url");//since we have entered correct login 
    details we are now being directed to the home page

              exit();
              } else {
              $errors = $data;
              }
              }

              //create a sopace between the button and the error messages
              //echo'<div class="spacer"></div>';

              if (!empty($errors)) {
              echo '<br/> <p class="error">The following errors occurred: <br  
    />';

              //foreach is a simplified version of the 'for' loop
              foreach ($errors as $err) {
              echo "$err <br />";
              }

              echo '</p>';
              }

              //this is the end of the <div> that contains the form
              echo '</div>';

              /* */
            ?>
        </div>

    <?php
    include 'Footer.php';
    ?>

1 个答案:

答案 0 :(得分:3)

请参阅session_start documentation备注部分。修改您的代码如下:

<?php 
// Start the session before ANY output.
// Start the session always - there's little / no value to only starting sometimes.
session_start(); ?>
<html>

<?php
/* static $called = FALSE;
if (!$called)
{*/

/*$called = true;
}*/

include_once 'debugging.php';
?>
<head>

session_start必须在任何输出发送到浏览器之前运行。另外,将它放在if语句中是没有价值的,所以保持简单并将它放在任何输出之前一直运行的地方。