8 Replies - 1991 Views - Last Post: 30 March 2014 - 08:18 PM Rate Topic: -----

#1 grizrule  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 29-December 13

session data not carrying from page to page

Posted 30 March 2014 - 05:36 PM

When a user uses the login system on my website and the credentials submitted are correct, a session and session data is set. But the sesion data is not being carried over from page to page. At the login script I printed out the session array and it showed everything was being set. But when I went to a different page with the same code that prints the session, it shows there is nothing in the session. (Yes, I am inserting session_start() at the beginning of every php script that needs it)

login.php

<?php

session_start();

require("config.php"); 

if(!empty($_POST)) 
{ 
    // This query retreives the user's information from the database using 
    // their username. 
    $query = " 
        SELECT 
            id, 
            username, 
            password, 
            salt, 
            email 
        FROM users 
        WHERE 
            username = :username 
    "; 

    // The parameter values 
    $query_params = array( 
        ':username' => $_POST['username'] 
    ); 

    try 
    { 
        // Execute the query against the database 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 

        die("Failed to run query: " . $ex->getMessage()); 
    } 


    $login_ok = false; 


    $row = $stmt->fetch(); 
    if($row) 
    { 

        $check_password = hash('sha256', $_POST['password'] . $row['salt']); 
        for($round = 0; $round < 65536; $round++) 
        { 
            $check_password = hash('sha256', $check_password . $row['salt']); 
        } 

        if($check_password === $row['password']) 
        { 
            // If they do, then we flip this to true 
            $login_ok = true; 
        } 
    } 

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again 
    if($login_ok) 
    { 

        unset($row['salt']); 
        unset($row['password']);  
        $_SESSION['USER'] = $row; 


        /* print_r($_SESSION); */

        session_write_close();
        header("Location: index.php"); 
        // Redirect the user to the index page. 
        die("Redirecting to the home page."); 
    } 
    else 
    { 
        print("Login Failed."); 

        $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        header("Location: login.php");
        exit;
    } 
} 
echo $message; 
?>


This next script checks to see if the session is set. If it isn't, it redirects the user to the login page.

<?php

/*** begin the session ***/
session_start();

if(!isset($_SESSION['USER']))
{ 
    // If they are not, we redirect them to the login page. 
    header("Location: ../login.php"); 
    exit;
} 
?>


Is This A Good Question/Topic? 0
  • +

Replies To: session data not carrying from page to page

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,707
  • Joined: 18-April 07

Re: session data not carrying from page to page

Posted 30 March 2014 - 05:48 PM

Are you running this code on your local machine or using a web host? If your local machine, make sure that the path specified as the session.save_path value in php.ini is accessible and that PHP has permissions to access that folder. Sometimes if PHP doesn't have correct permissions, or the path does not exist, it will not save the PHP session data and thus won't carry across pages.

If that seems to be fine, I suggest you try a really simple test example to make sure that it is not something in your code here. Quickly make two pages, each with session_start() and where one creates a session variable and the other page reads that session variable. This test will help tell you if it is the code or if there is indeed something wrong with how PHP is configured itself.

Hope this advice helps you out. :)
Was This Post Helpful? 1
  • +
  • -

#3 grizrule  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 29-December 13

Re: session data not carrying from page to page

Posted 30 March 2014 - 06:02 PM

I am running this on a web host. I took your advice and tested the session stuff. I made the two pages where one creates the session variable and data then redirects to the 2nd page where it echos the session variable. I found that the session data did not go over to the second page.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: session data not carrying from page to page

Posted 30 March 2014 - 06:46 PM

I like to ensure that session_start() is the very first line, although I know that a comment or blank before it shouldn't make any difference.

I would remove the following line after your header call as well:
die("Redirecting to the home page.");

Just as part of the debugging process I would try without this session_write_close();. Again, it shouldn't be an issue.

You are writing an entire $row to the session, try it with a simple text value.

And try different browsers. All part of the debugging process!

This post has been edited by andrewsw: 30 March 2014 - 06:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 grizrule  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 29-December 13

Re: session data not carrying from page to page

Posted 30 March 2014 - 07:26 PM

Thanks Andrew. Do you know of any other debugging processes? I can't seem to figure out what's wrong.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: session data not carrying from page to page

Posted 30 March 2014 - 07:41 PM

Lots :)

Check your browser's cookie settings.

As I mentioned, try with just $_SESSION['USER'] = 'testing'.

Create two of the simplest test-pages to work with, just setting and reading a session value. Edit: This is what Martyr said as well, great minds think alike :)

Put this
error_reporting(E_ALL);
ini_set('display_errors', '1');

at the top of your scripts so that you can see all errors during development.

Use print_r(), var_dump() and even just echo statements to examine your variables.
print_r($_SESSION);

.. will display the entire array - view the source of the page to read it clearly.

This post has been edited by andrewsw: 30 March 2014 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#7 grizrule  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 29-December 13

Re: session data not carrying from page to page

Posted 30 March 2014 - 07:50 PM

The first thing that I did was insert the error_reporting(E_ALL); ini_set('display_errors', '1'); into my login script and got 3 warnings related to sessions...

Warning: session_start(): open(D:\Temp\php\session\\sess_f5ja6athjhgm3c84kvmc7kgdr3, O_RDWR) failed: No such file or directory (2) in D:\Hosting\12059488\html\login.php on line 5

Warning: Unknown: open(D:\Temp\php\session\\sess_f5ja6athjhgm3c84kvmc7kgdr3, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (D:\Temp\php\session\) in Unknown on line 0

I understand what they mean...sort of. Would you mind explaining them just to clarify?

This post has been edited by grizrule: 30 March 2014 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: session data not carrying from page to page

Posted 30 March 2014 - 08:13 PM

Re-read Martyr2's post about session.save_path.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: session data not carrying from page to page

Posted 30 March 2014 - 08:18 PM

I'll take a wild guess that you need to specifically set this save path otherwise it is defaulting to a location that either doesn't exist or doesn't have the correct permissions. (Maybe some hosts default it to the same location as the file, or somewhere off root.)

Just guesswork though ;)

session_save_path

This post has been edited by andrewsw: 30 March 2014 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1