473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
14 1444
Motoma
3,237 Expert 2GB
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
snapple
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
snapple
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
snapple
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
3,237 Expert 2GB
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
snapple
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
3,237 Expert 2GB
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
snapple
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
snapple
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
3,237 Expert 2GB
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
snapple
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
3,237 Expert 2GB
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
snapple
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
3,237 Expert 2GB
: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

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

Similar topics

0
by: Taz | last post by:
comp.lang.php, I have recently launched the new Planet-Tolkien.com, one would think that writing a message board from scratch and a dynamic weather system, a simple session login would be the...
0
by: Phil Powell | last post by:
What is the most standardized method of utilizing the CURL functions in PHP (version 4.3.2) to be able to retrieve the contents of a remote URL that happens to be dependent upon $_SESSION for its...
4
by: comp.lang.php | last post by:
This is an urgent request (as always) generate_admin_customer_position_dropdown($customerResult, $customerResult->id); print_r($_SESSION); This code will generate an HTML dropdown as...
21
by: axlq | last post by:
Someone please tell me if I've discovered a PHP bug. I'm sitting in front of several computers on my home network, behind a NAT firewall/router. I am testing my web site on these different...
1
by: bartdlr | last post by:
Hi, I got a problem to access the $_SESSION variable in my script. I wrote some login code: if the user logs in with his name and password a user object is stored in the $_SESSION variable. ...
5
by: damezumari | last post by:
When a user logs in to my site http://iwantyourquestion.com I set $_SESSION to true if his username and password are OK. When he calls a page I check if $_SESSION is true. If it not I ask him to...
19
nathj
by: nathj | last post by:
Hi, I am trying to get $_SESSION to work on my site. In order to learn this an dunderstand it better I have built two very simple test pages to see if i can access $_SESSION on both pages. ...
4
by: dpinion | last post by:
Greetings, I am trying to do some simple session stuff. However it does not seem as though the session variable is being created for my site. I am running the latest version of PHP and apache that...
9
nathj
by: nathj | last post by:
Hi, I am having a spot of bother with the use of $_SESSION. They are gernally working absolutely fine with one exception. On my form I have a capthca security image, when this image is built...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.