Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 19th, 2008, 12:47 AM   #1
MrMan9879
Programmer
 
MrMan9879's Avatar
 
Join Date: Sep 2005
Location: Nanaimo, BC, Canada
Posts: 97
Rep Power: 0 MrMan9879 is an unknown quantity at this point
Send a message via MSN to MrMan9879
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?

Last edited by MrMan9879; Feb 19th, 2008 at 12:48 AM. Reason: punctuation error
MrMan9879 is offline   Reply With Quote
Old Feb 19th, 2008, 1:33 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 19th, 2008, 5:30 PM   #3
MrMan9879
Programmer
 
MrMan9879's Avatar
 
Join Date: Sep 2005
Location: Nanaimo, BC, Canada
Posts: 97
Rep Power: 0 MrMan9879 is an unknown quantity at this point
Send a message via MSN to MrMan9879
Re: Sessions Problem

Alright, I guess I misunderstood what sessions did... thanks for your help.
MrMan9879 is offline   Reply With Quote
Old Feb 19th, 2008, 8:08 PM   #4
MrMan9879
Programmer
 
MrMan9879's Avatar
 
Join Date: Sep 2005
Location: Nanaimo, BC, Canada
Posts: 97
Rep Power: 0 MrMan9879 is an unknown quantity at this point
Send a message via MSN to MrMan9879
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?
MrMan9879 is offline   Reply With Quote
Old Feb 19th, 2008, 9:10 PM   #5
Syntax_Error
Programmer
 
Syntax_Error's Avatar
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0 Syntax_Error is on a distinguished road
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.
Syntax_Error is offline   Reply With Quote
Old Feb 19th, 2008, 9:22 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Sessions Problem

I'd like to see the code before that segment - problem could be up there.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 19th, 2008, 9:28 PM   #7
MrMan9879
Programmer
 
MrMan9879's Avatar
 
Join Date: Sep 2005
Location: Nanaimo, BC, Canada
Posts: 97
Rep Power: 0 MrMan9879 is an unknown quantity at this point
Send a message via MSN to MrMan9879
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.
MrMan9879 is offline   Reply With Quote
Old Feb 19th, 2008, 9:44 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Sessions Problem

Have you called session_start?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 19th, 2008, 9:45 PM   #9
MrMan9879
Programmer
 
MrMan9879's Avatar
 
Join Date: Sep 2005
Location: Nanaimo, BC, Canada
Posts: 97
Rep Power: 0 MrMan9879 is an unknown quantity at this point
Send a message via MSN to MrMan9879
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.
MrMan9879 is offline   Reply With Quote
Old Feb 19th, 2008, 9:59 PM   #10
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Challenging Programming Problem - "Pinball Ranking" Sane Coder's Corner Lounge 38 Jan 15th, 2008 5:16 PM
Problem solving ReggaetonKing Software Design and Algorithms 7 Jan 4th, 2008 1:49 PM
Storing BLOBs in a database - problem jonyzz Other Programming Languages 8 Jan 31st, 2007 4:38 AM
Changing icons problem Pedja C# 8 Mar 25th, 2006 8:03 AM
cgi/perl script + IE problem joyceshee Perl 2 Jan 24th, 2006 11:10 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:05 AM.

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