Search code examples
phpsessionmamp

PHP Sessions not saved


My sessions aren’t working. Everytime I reload the page it generates a new session id and deletes all session variables.. I am not sure if there is an error in my code or in my configuration. I am using MAMP on Mac as local server software with php 5.2.17

Every php file includes the layout_top.php at first. the layout_top starts the session every time:

<?php

        session_start();

    ?>

also there is a log in site like this:

<?php

include 'layout_top.php';

include 'db_connect.php';

$username = $_POST["li_username"]; 
$pw = $_POST["li_passwort"];

    if ($username == "" || $pw == "") {
        echo "Fülle bitte alle Felder aus!";
        }

    else {

        $check_pw_query = mysql_query("SELECT password FROM user WHERE username = '$username'") or die(mysql_error());

        $check_pw = mysql_fetch_assoc($check_pw_query);

        if ($pw == $check_pw{'password'}) {
            echo "Juhu! </br>";



            $first_name_query = mysql_query("SELECT first_name FROM user WHERE username = '$username'") or die(mysql_error());

            $first_name = mysql_fetch_assoc($first_name_query);

            $second_name_query = mysql_query("SELECT second_name FROM user WHERE username = '$username'") or die(mysql_error());

            $second_name = mysql_fetch_assoc($second_name_query);        

            $id_query = mysql_query("SELECT id FROM user WHERE username = '$username'") or die(mysql_error());

            $id = mysql_fetch_assoc($id_query);

            if (!isset($_SESSION['username'])) {
                $_SESSION['username'] = $username;
            }

            if (!isset($_SESSION['first_name'])) {
                $_SESSION['first_name'] = $first_name{'first_name'};
            }    

            if (!isset($_SESSION['second_name'])) {
                $_SESSION['second_name'] = $second_name{'second_name'};
            }

            if (!isset($_SESSION['id'])) {
                $_SESSION['id'] = $id{'id'};
            }
            if (!isset($_SESSION['gender'])) {
                $_SESSION['gender'] = $gender{'gender'};
            }

        }

    }

There it sets the session variables.. but if I reload the page all session variables are gone. Hope you understand my problem. Thanks for helping.

EDIT: Here is the phpinfo(), maybe this will help finding the problem..

session.auto_start          Off Off
session.bug_compat_42   On  On
session.bug_compat_warn On  On
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly Off Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    no value    no value
session.entropy_length  0   0
session.gc_divisor  100 100
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character|4   4
session.hash_function   0   0
session.name            PHPSESSID|PHPSESSID
session.referer_check   no value    no value
session.save_handler    files   files
session.save_path   /Applications/MAMP/tmp/php  /Applications/MAMP/tmp/php
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    Off Off
session.use_trans_sid   0   0

EDIT 2:

Strict Standards: session_start() [function.session-start]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /Applications/MAMP/htdocs/network/layout_top.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/network/layout_top.php:1) in /Applications/MAMP/htdocs/network/layout_top.php on line 6

Strict Standards: session_start() [function.session-start]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /Applications/MAMP/htdocs/network/layout_top.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/network/layout_top.php:1) in /Applications/MAMP/htdocs/network/layout_top.php on line 6

Solution

  • why you are taking so many queries... try this

           $query = mysql_query("SELECT * FROM user WHERE username = '$username'") or die(mysql_error());
    
            while($row = mysql_fetch_array($first_name_query))
            {
    
            if (!isset($_SESSION['username'])) {
                $_SESSION['username'] = $row['username'];
            }
    
            if (!isset($_SESSION['first_name'])) {
                $_SESSION['first_name'] = $row['first_name'];
            }    
    
            if (!isset($_SESSION['second_name'])) {
                $_SESSION['second_name'] = $row['second_name'];
            }
    
            if (!isset($_SESSION['id'])) {
                $_SESSION['id'] = $row['id'];
            }
            if (!isset($_SESSION['gender'])) {
                $_SESSION['gender'] = $row['gender'];
            }
            }
    

    and I will not suggest you to save multiple data on session .. just save userid in your session variable, because session variables are saved on server so it puts load on your server..

    If your session directory (most like /tmp as you said) is not writable, then it won't be able to save and will have to regenerate a new one each time. Here is how you can verify it:

    if ( !is_writable(session_save_path()) ) { echo 'Session save path "'.session_save_path().'" is not writable!'; }

    and if it is not writable than please change the directory permissions...

    for timezone error.. please look at this link.. you need to set default timezone http://pl.php.net/manual/en/function.date-default-timezone-set.php

    and for header already sent error please check How to fix "Headers already sent" error in PHP

    so you will be done.. :)

    please let me know if you want any further guidance..