session getting lost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

session getting lost

Post by etsted »

all of my php pages works fine localy on my computer whit session, but wheni upload them to a server they dont work. I search a little and read that my sessions might get lost, i didnt understand what that meant. Can somebody tell me how i can fix that, so that i can upload my pages to a server whitout a session problem? And give examples.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: session getting lost

Post by Celauran »

etsted wrote:but wheni upload them to a server they dont work
Can you elaborate on that?
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

Re: session getting lost

Post by etsted »

the sessions doesnt store user information, so the webpage keeps on logging me out. But it doesnt do that if i run the webpage localy.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: session getting lost

Post by Celauran »

Have you inspected the $_SESSION array? Are any errors being displayed (or logged)? Is error reporting on?
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

Re: session getting lost

Post by etsted »

what u mean? it works localy when i run the script on my pc no errors.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: session getting lost

Post by Celauran »

So we're back to "it doesn't work" with no additional detail. We can't help you if you won't even answer our questions. Got nothing to work with here.
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

Re: session getting lost

Post by etsted »

i have tried to logg all the errors, but nothing shows up. I have read somewhere that something might be wrong whit the time settings, but i dont know.
If you like read this: http://strangerzlog.blogspot.it/2012/04 ... ns-on.html
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: session getting lost

Post by hybris »

Are you using header(location: ...) to redirect? If so dont use the full url http://www.mysite.com/secure page but local way like ../securepage.php

Atleast that worked for me when I had similar trouble..dunno why since if I logged in and then pasted the url to the secure page in the new window i got in (as expected since i was logged in) but when i used the full adress in my header it didnt work.
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

Re: session getting lost

Post by etsted »

no, i am not using headers.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: session getting lost

Post by Christopher »

Is the session data actually being written on the server? Perhaps it is a permissions or configuration problem.
(#10850)
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

Re: session getting lost

Post by etsted »

Code: Select all

<!DOCTYPE html><?php session_start();?>
<html>
    <head>
        <meta charset="UTF-8" />
        <link href='style/style.css' rel='stylesheet' type="text/css" />
        <title>Login</title>
        <meta name='description' content='login to itsnature' />
        <meta name='keywords' content='login' />
    </head>
  <body>
    <h1>Login</h1>
<?php
require_once "connect.php";
// login form
            $login = "<form action='login.php' method='POST' name='login1' class='forgotpass'>
                <input type='text' name='username' placeholder='Username' /> <br />
                <input type='password' name='password' placeholder='Password' />
                <input type='submit' name='submit' value='Login' /> |
                <a href='register.php'>Register</a>
            </form>";
if(isset($_SESSION['username']))
{
    $username = $_SESSION['username'];
}
// sets a navigator
if(isset($username))
{
 include "navigator/navigator_login.php";
 echo "hei";
} else {
 include "navigator/navigator.php";
 echo "hei feil";
}

if(isset($username))
{
    echo "<p>Welcome $username.</p> <br />";
}
else
{
    if(isset($_POST['username']) && isset($_POST['password']))
    {
        $username = mysqli_real_escape_string($con, $_POST['username']);
        $password = mysqli_real_escape_string($con, $_POST['password']);
    }
    if(isset($username) && isset($password))
    {
        $password = md5($password);
        $query = mysqli_query($con ,"SELECT * FROM users WHERE username='$username' && password='$password'") or die(mysql_error());
        $numrows = mysqli_num_rows($query);
        if($numrows != 0)
        {
            $_SESSION['username']=$username;
            echo "<p>You have been logged in.</p> <br />";
        }
        else
        {
            echo "<p>Wrong password or username.</p>$login";
        }
    }
    else
    {
        echo "<p>You must register an account in order to login.</p>$login";
    }
}
mysqli_close($con);
?>
    </body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: session getting lost

Post by Celauran »

etsted wrote:

Code: Select all

<!DOCTYPE html><?php session_start();?>
There's your problem. session_start sends out headers, which you've already sent. You should be seeing a "headers already sent" warning. Try placing the session_start call before sending any output to the browser.

Code: Select all

<?php session_start(); ?>
<!DOCTYPE html>
etc.
jangmi
Forum Newbie
Posts: 11
Joined: Sat Mar 08, 2014 7:39 am

Re: session getting lost

Post by jangmi »

You can check /tmp folder in your server! This directory must be written
Post Reply