![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Re: Sessions Problem
|
|
|
|
|
|
#3 |
|
Programmer
|
Re: Sessions Problem
Alright, I guess I misunderstood what sessions did... thanks for your help.
|
|
|
|
|
|
#4 |
|
Programmer
|
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? |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2008
Posts: 9
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Re: Sessions Problem
I'd like to see the code before that segment - problem could be up there.
|
|
|
|
|
|
#7 |
|
Programmer
|
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. |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Re: Sessions Problem
Have you called
session_start? |
|
|
|
|
|
#9 |
|
Programmer
|
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. |
|
|
|
|
|
#10 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |