简体   繁体   中英

session_start() not working properly

I hope you are doing great, I'm making this website with login accounts apparently, I have registered my users in a local database and in a server database. My HTML/PHP code doesn't show any errors when I run it.I have checked my DB connection. it's correct. The website allows me to sign in with any user and password. It doesn't seem to validate my entered data properly. Although I checked my SQL command. I wonder if you could help me with this guys. You are the best! :) thanks in advance, Cheers here is a useful piece of my code: My header - 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>';
      } 
    ?>

Progress - Feedback:

Ok guys, I tried what you told me to do. It started to make me sign in automatically. Probably the session['logged'] variable declaration is considered to be true. I set it to be true only if the user login from the login page. but it is not functioning in that way. Here is my login page code:

<?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';
    ?>

See the notes section of the session_start documentation . Revise your code as follows:

<?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 must run before any output is sent to the browser . Additionally, there's no value in having it in an if statement, so keep it simple and put it where it runs consistently before any output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM