Johnathan Brown
Johnathan Brown

Reputation: 713

PHP Session issues, perhaps lost data?

I'm having some issues using sessions on my test website (Running it on WAMP server locally, using PHP php5.3.13) I have checked my php.ini to make sure that sessions are actually being saved, which they are:

C:\wamp\tmp

Basically, when the user logs in it shows, Welcome back, .$username so when I log in with the user "John", it shows this accordingly. Now, when I leave the login page and go back to it this sessions is somehow being lost. (And yes, I am using session_start at the top of every page).

Here is my code;

index.php

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Codecall Tutorials - Secured Login with php5</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>

        <?php include "header.php" ?>

         <div id="wrapper">
             <form method="post" action="">
             <h2>Log In</h2>
             <div id="underline"></div>
                 <ul>
                     <li>
                         <label for="usn">Username : </label>
                         <input type="text" maxlength="30" required autofocus name="username" />
                     </li>

                     <li>
                         <label for="passwd">Password : </label>
                         <input type="password" maxlength="30" required name="password" />
                     </li>
                     <li class="buttons">
                         <input type="submit" name="login" value="Log me in" class="xbutton" />
                            <input type="button" name="register" value="Forgot Password?" onclick="location.href='passrecover.php'" class="xbutton" />
                     </li>

                 </ul>
             </form>

            </div>

    </body>
</html>

<?php include "login.php" ?>

And my login.php page:

<?php 

if($_POST){

        if(empty($_POST['username']) && empty($_POST['password'])) {
            echo 'Please enter all fields';
        }else {

        $username = $_POST['username'];
        $password = $_POST['password'];

        if($password !== $password){
            echo 'Your password is wrong';
        }else {

            $db_name = 
            $db_user = 
            $db_pass = 

            $conn = new PDO('mysql:host=localhost;dbname=XXXXX', 'XXXXX', 'XXXXX', // My bd details have been removed for this post, for security issues obviously
                array( PDO::ATTR_PERSISTENT => true )
        );

$stmt = $conn->prepare("SELECT username,password from members WHERE username = ? AND password = ?");

            $stmt = $conn->prepare("SELECT username,password FROM users WHERE username = ? AND password = ?");
            $stmt->execute(array($username, $password));

            if($stmt->rowCount() === 1 )
            {   
                $_SESSION['name']= $username;
                echo 'Welcome back '. $_SESSION['name'];
                //echo '<META HTTP-EQUIV="Refresh" Content="0; URL=usercp.php">';
            }else {
                echo 'Username or Password incorrect.';
            }           
        }
    }
}

?>

So, when I originally log in it shows the $_SESSION['name']; just fine, but when I move page and go back to it, it no longer shows it. (My other pages also have session_start(); ) My original assumption was that my code was wrong, or that my php.ini file wasn't saving any data. What is going wrong here?

Upvotes: 1

Views: 243

Answers (3)

Tristan
Tristan

Reputation: 2399

The reason it no longer shows is because when you go to another page, you aren't processing that block of code anymore. Because $_POST is empty on a regular page load, so you aren't echoing anything out. Try adding, var_dump($_SESSION); at the top of your page and then load something.

Try this right after your session_start();,

if(!empty($_SESSION['name'])) { 
   echo "Hello {$_SESSION['name']}"; 
}

Upvotes: 1

Hugo
Hugo

Reputation: 170

Perhaps you're destroying the session somewhere in the script.

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

You need to add session_start() on your login.php too

Upvotes: 2

Related Questions