By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

$_SESSION only seems to work part of the time for my site

P: 11
I created a game called Mystic Crusade (www.mysticcrusade.com) and it tends to work perfectly except for one thing, sometimes it doesn't register the session, i guess. For some reason, some of my users have to log in several times before they are logged in to stay. This is what I mean...

Most users log in and it takes them to their "home" page, then they can move around and do stuff in the game.

But some users log in, it takes them to their "home" page (which you have to be logged in to see) but when they try to move around the page, it's like as if they were not logged it. If you are not logged in, it kicks you off the game and you have to sign in again. These users will sign in again and again and it will eventually work. This problem has never affected me, but it does effect some users every time.

I use $_SESSION to register the user, the it takes them to their 'home' page via
[php] header('Location: home.php');


exit;[/php]

and I do start the session on every page with session_start();

Please help me, I just don't understand why it seems to work only some of the time
Feb 26 '07 #1
Share this Question
Share on Google+
14 Replies


Motoma
Expert 2.5K+
P: 3,235
This problem may have more to do with how you are destroying your sessions.
Tell me, what method are you using for keeping the session id?
Feb 26 '07 #2

P: 11
This problem may have more to do with how you are destroying your sessions.
Tell me, what method are you using for keeping the session id?
I'm sorry, what do you mean? I assume that when you use $_SESSION it keeps the information until the browser is closed.

Here is an example on how I set them up
[php]
$name='collin';
$_SESSION['user'] = $name;
[/php]
Now, $name actually equals the username of the user, that was just an example.

to end the sessions I do this...
[php]
if (isset($_SESSION['user'])) {
unset($_SESSION['user']);
}
[/php]
which is located at logout.php
Feb 26 '07 #3

P: 11
if someone tried to look at my website earlier, it was down because I was trying to figure it out, but I failed to do so. Someone please still help me.
Feb 27 '07 #4

P: 11
if someone tried to look at my website earlier, it was down because I was trying to figure it out, but I failed to do so. Someone please still help me.
Come on, there has to be someone that can help me
Feb 28 '07 #5

Motoma
Expert 2.5K+
P: 3,235
Come on, there has to be someone that can help me
I'm sorry, but you still haven't answered my question as to how you are handling Sessions. Are you using a Cookie to store the Session ID, or are you passing it around via POST or GET?

The way I usually log out is like so:
[php]
function logout()
{
$_SESSION = array();
session_regenerate_id(true);
session_destroy();
}
[/php]

Methinks that your users may have a problem with their PHPSESSID Cookie getting removed, either by your system or by clearing the cache.
Feb 28 '07 #6

P: 11
I've never really totally understood sessions, so I would have to guess that I use cookies. I defiantly don't do anything with post and get as far as sessions go.

I changed the way I delete sessions to how you showed me you did it. I currently don't know if that fixed anything (The problem doesn't occur with me).

Um... so I hope I answered your question on how I handle the sessions.

When I make the sessions I say something like

$_SESSION['username'] = $username;
Feb 28 '07 #7

Motoma
Expert 2.5K+
P: 3,235
I would suggest, then, to read the article about Session Handling Functions. In particular, focus on the section about passing the session id.
Feb 28 '07 #8

P: 11
Right, I knew all of that. I did some other research before I saw this post and found out that I pass the sessions by cookies. This really confuses me because it works after a few attempts at it. I'll see if changing the way the sessions are deleted does anything
Feb 28 '07 #9

P: 11
So yeah, I talked to my friend who has the problem and it still occurs. He logged on to the site (took him two tries) then he logged out, closed the browser, reopened it, and it still took him two tries to log in.
Feb 28 '07 #10

Motoma
Expert 2.5K+
P: 3,235
So yeah, I talked to my friend who has the problem and it still occurs. He logged on to the site (took him two tries) then he logged out, closed the browser, reopened it, and it still took him two tries to log in.
What I think is happening is that your first login is not correctly setting your Session ID in your client's Cookie.

If you have Firefox, you can watch your cookies as you go through your site.
Tools->Options->Privacy->Show Cookies.
Enter in your server's URL as a filter.
Watch what your PHPSESSID cookie contains the first time you try logging in (unsuccessfully), and compare it to the second time you log in (success).
Feb 28 '07 #11

P: 11
Okay so talking to my friend, we found out that it was an entirely different problem. http:www.mysticcrusade.com looks the exact same as http://mysticcrusade.com, but it handles the sessions completely differently. My friend had the site bookmarked as mysticcrusade.com instead of www.mysticcrusade.com, so he would have to log in twice. Now i'm wondering, how do i redirect people from mysticcrusade.com to www.mysticcrusade.com to prevent the problem from happening again? is there like a

(pseudocode)
if(header="http://mysticcrusade.com")
{
//make header http://www.mysticcrusade.com
}

edit: In fact, the site works perfectly fine if I leave off the www. for any website I want to go to. I don't even know how that got there
Feb 28 '07 #12

Motoma
Expert 2.5K+
P: 3,235
Okay so talking to my friend, we found out that it was an entirely different problem. http:www.mysticcrusade.com looks the exact same as http://mysticcrusade.com, but it handles the sessions completely differently. My friend had the site bookmarked as mysticcrusade.com instead of www.mysticcrusade.com, so he would have to log in twice. Now i'm wondering, how do i redirect people from mysticcrusade.com to www.mysticcrusade.com to prevent the problem from happening again? is there like a

(pseudocode)
if(header="http://mysticcrusade.com")
{
//make header http://www.mysticcrusade.com
}

edit: In fact, the site works perfectly fine if I leave off the www. for any website I want to go to. I don't even know how that got there
This might not be an entirely different problem. You see, a Cookie has a hostname associated with it. If one you are at mysticcrusade.com and create a Cookie, then redirect to www.mysticcrusade.com, and look for the Cookie, it will not be there.

There is a tool you can use, depending on the level of control you have over your server. If you are using Apache, mod_rewrite will allow you to create rules for URLs. This is the preferred method.

The other option you have is to put this code on your opening page:
[php]
if($_SERVER['HTTP_HOST'] != "www.mysticcrusade.com") header("Location: www.mysticcrusade.com");
[/php]

The problem with this method is that it will not work if a user bookmarks a specific page on your site.
Feb 28 '07 #13

P: 11
:D Okay! Everything should turn out all right then. I asked my server to do that to apachie for me, and i added the code
[php]
if($_SERVER['HTTP_HOST'] != "www.mysticcrusade.com") header("Location: http://www.mysticcrusade.com");
[/php]

and that seems to work alright. Hopefully my problem is totally fixed now. Thank you SO much for your help, it's amazing how such a simple thing can cause such a great deal of stress. Now Mystic Crusade is the best website ever made again, yay!

haha, seriously, thank you very much for your help.
Feb 28 '07 #14

Motoma
Expert 2.5K+
P: 3,235
:D Okay! Everything should turn out all right then. I asked my server to do that to apachie for me, and i added the code
[php]
if($_SERVER['HTTP_HOST'] != "www.mysticcrusade.com") header("Location: http://www.mysticcrusade.com");
[/php]

and that seems to work alright. Hopefully my problem is totally fixed now. Thank you SO much for your help, it's amazing how such a simple thing can cause such a great deal of stress. Now Mystic Crusade is the best website ever made again, yay!

haha, seriously, thank you very much for your help.
Glad to hear that things work for you now, and I'm happy I was able to help.
Hope things go well for your site; come back if you need any more help.
Feb 28 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.