Eric Random
Eric Random

Reputation: 158

Session not saving for whole site

I got a really weird problem with the php session functions.

First of all: it works only in certain browsers. Like random selected: FF on windows, but not on Mac, Chrome on both platforms, safari doesnt work IE does however.

I have a login script, where I create a session if the login data is correct:

session_start();
$_SESSION['username'] = $user_info['username'];

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

When I do print_r in the login script, after setting the session variables, it works for all browsers. But after redirecting the user to the main site, the session is empty for some browsers....

I never had such a problem and I really dont know what to do...

Upvotes: 0

Views: 75

Answers (1)

Dorvalla
Dorvalla

Reputation: 5240

session_start();
$_SESSION['username'] = $user_info['username'];

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

If this is what starts every page, I can understand you dont see anything anymore after the refresh, cause simply, what you do is you start your session, you set the $_SESSION variable Username to $User_info with the variable username. And you do that every time.

But consider this: your variable isn't specified at all at this point. Maybe you set it up when you logged in, however, you clear it inmideatly on the next refresh, cause you tell it use that $user_info['username'] again. In this case, with nothing called on forhand, it will be an empty variable. Thus, your username will be blank.

Your session['username'] will also be blank, because you tell it to take that information.

Since you set it when you log in, there is no need to respecify it, cause your session['username'] is already set

edit

so I ask now, what do you put to your database? Do you save the SessionId in your DB? Cause then you can make something like this.

session_start();

$Session=session_id();  
$result1 = LoadSessionData($Session);
$count=mysqli_num_rows($result1);
if($count=="0"){
    $Time=time();
    $Ip = $_SERVER['REMOTE_ADDR'];
    StartSessionData($Session,$Time,$Ip);
    $_SESSION['Username'] = '';
}else{
    while ($row1 = mysqli_fetch_array($result1)){       
        $UserName = $row1['GebruikerNaam'];
        if ($UserName == ""){
            $_SESSION['Username'] = '';
        }
        $Time=time();
        $Time_check=$Time-3600;
        $Ip = $_SERVER['REMOTE_ADDR'];
        OnlineChecker($Session,$UserName,$Time,$Ip);
    }
}

I hope you can follow it, but i ll try to explain. It checks if the sessionId is in the database, if not, add the session id in the database together with timestamp, ip and session username, which is set to blank.

Else, i check what i get back, if it has a username, use that. If it's blank, the sessionusername will remain blank, cause it checks later on with that username if I am logged in. Then I update the database with the username, time, etc.

Upvotes: 1

Related Questions