Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Sessions Problem (http://www.programmingforums.org/showthread.php?t=15212)

MrMan9879 Feb 19th, 2008 1:47 AM

Sessions Problem
 
I'm trying to learn more about PHP sessions, as I'm going to be using them in a website I'm building soon.

Anyways, my problem with them is that I'm wondering how I can make them last after the user's browser is closed.

Here is a code example:

:

<?php
start_session();

$_SESSION['name']='Andrew';
$_SESSION['country']='Canada';
$_SESSION['age']=16;

?>


Then obviously I would view the session on another page with code like this:

:


<?php
start_session();

echo $_SESSION['name'];
//and so on

?>


Now, as I understand it... sessions are stored in a cookie on the user's browser so that other scripts can use them and such. The above code always works, and I can go to another page and come back and it still works. But, if I close my browser and I come back to the page that views the session variables, they aren't set anymore.

I've tried changing settings like session:cookie.lifetime, session:cookie.expire, etc. but so far, I've pretty much had no luck. I've searched with google thoroughly and checked the PHP manual, and I haven't found anything that's helped me out.

So, anyone have anything that'll help me out? Basically, I'm going to be using sessions with MySQL for authentication and such, so I figured it would be nice to have users logged in when they come back after closing their browsers. I assume this is possible, as I've seen it before, but perhaps the websites were using something different?

Ooble Feb 19th, 2008 2:33 AM

Re: Sessions Problem
 
The whole point of a session cookie is that it lasts for the duration of your session - i.e. until you close your browser, at which point it nukes all session cookies. If you want persistent cookies, use $_COOKIE and the setcookie function.

MrMan9879 Feb 19th, 2008 6:30 PM

Re: Sessions Problem
 
Alright, I guess I misunderstood what sessions did... thanks for your help.

MrMan9879 Feb 19th, 2008 9:08 PM

Re: Sessions Problem
 
Alright, so now I'm working on the actual authentication script for my website, and I'm using cookies. I tested out cookies, and I had them working, but now when I try it, it isn't working.

Here is my code:

:

while ($result=mysql_fetch_array($query)) {
                //loop through the usernames and passwords
                if (($new_user == $result['username']) && ($new_password == $result['password'])) {
                        //if the user matches a user in the database, setup the cookies
                        setcookie('user_name', $new_user, time()+3600);
                        setcookie('password', $new_password, time()+3600);

                        //create the session variables
                        $_SESSION['user_name'] = $_COOKIE['user_name'];
                        $_SESSION['password'] = $_COOKIE['password'];
                       
                        print_r($_SESSION);
                        print_r($_COOKIE);
                       
                        //create normal variables
                        $user = $_SESSION['user_name'];
                        $password = $_SESSION['password'];
                       
                        //end the loop
                        break;
                }
        }


I basically just have it looping through a mysql query looking to match a username and password, and then it puts it into a cookie, then a session cookie and a variable. It wasn't working, so I used print_r to view what's in the $_SESSION and $_COOKIE global array, but there was nothing in it.

Any thoughts on why exactly it doesn't work?

Syntax_Error Feb 19th, 2008 10:10 PM

Re: Sessions Problem
 
are the passwords in your database encrypted? or rather are you encrypting the pass on its way to the query? any mismatch of encryption would throw it.

Ooble Feb 19th, 2008 10:22 PM

Re: Sessions Problem
 
I'd like to see the code before that segment - problem could be up there.

MrMan9879 Feb 19th, 2008 10:28 PM

Re: Sessions Problem
 
Well, I used the sha1() function to encrypt the password, but I have the same thing in the database.

For example, the password for the user I am testing with is asdfasdf, which is changed to: 92429d82a41e930486c6de5ebda9602d55c39986

The password in the database is also: 92429d82a41e930486c6de5ebda9602d55c39986.

I don't think the password is a problem, because it gets through the loop and it gets to the part where it has

print_r($_SESSION)

But, it shows a blank array.

Ooble Feb 19th, 2008 10:44 PM

Re: Sessions Problem
 
Have you called session_start?

MrMan9879 Feb 19th, 2008 10:45 PM

Re: Sessions Problem
 
I'm pretty sure I have... and I'll check now... but is session_start() necessary for cookies to work?

EDIT: Just checked... printing the $_COOKIE array does display a session ID, therefore I can safely assume that I started it.

Dameon Feb 19th, 2008 10:59 PM

Re: Sessions Problem
 
I'm not going to comment on the rest of it yet, but I will say that you should look in to WHERE clauses in SQL. Looping through all user records is bad. Tell the database which one you are looking for.


All times are GMT -5. The time now is 4:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC