/    Sign up×
Community /Pin to ProfileBookmark

Disappearing session variables

I’m so confused by session variables. They keep appearing and disappearing.

I’ve made a simplified example of the problem which you can view here.

[URL]http://www.netcartoon.net/naaas/newtry/start.php[/URL]

Basically, I am trying to make a log-in page, which will re-direct the user to the page they came from once they log-in. The problem is, when I re-direct them the session variables disappear, so it doesn’t register them as logged in.

If I use a hyperlink to return them to the original page it works ok…so I was thinking it’s a problem with the re-direct? But if you look at the login1.php – it’s TOTALLY weird. It has two lines right after each other. The first one says echo the session variable ‘returnurl’ and the second creates a dynamic link using the same variable. The echo line shows nothing, but the link works fine.. Why?

I just don’t understand session variables. If you can take some time to look at this example and help me understand what is actually happening I would be so grateful.

Thanks

Toby

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@tobyw_1969authorJan 02.2004 — Even stranger - i tried it again and it works from this link....is this something to do with caching? I can't understand why it works sometimes and not others...
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzJan 02.2004 — question - why do you have the exit() on there? doesnt really need to be there... mayby that is effecting your variables in some way, i dunno. also - why do you use http variables? you can use cookie based ones that do the same thing without the messy url.

[code=php]if (!isset($_SESSION['loggedin'])) {
session_start();
$_SESSION['loggedin'] = "true";
}
else {
echo "You are already logged in";
}[/code]


on the protected page, do this:

[code=php]session_start();

if (!isset($_SESSION['loggedin'])) {
die("You must be logged in");
}
else {
if ($_SESSION['loggedin'] == true) {
echo "You are logged in";
}
}[/code]
Copy linkTweet thisAlerts:
@tobyw_1969authorJan 02.2004 — Thanks Sux.

I put the exit() in because that seemed to be what it suggested on the tutorial I read online - it said something about making sure no other code got executed AFTER the re-direct... so perhaps it's superfluous.

Does $_SESSION mean cookie instead of URL based session? In my book it says PHP will try to use cookies if it can or if not then will use the URL, but I only ever notice it using the URL. I really am finding it tough to find a decent site on sessions.

Anyway - the good news is that my site suddenly all started working again this morning. Without me changing anything. I can only assume it is something to do with ISP caching. I know NTL cache files at their end to speed things up...I can't understand why it would stop woring one dayand be ok the next otherwise.

Anyway, thanks again for your help. Programming a forum function is not easy! But I can't afford to buy one.

If you are interested, you can see the result here

http://www.netcartoon.net/naaas/quiz.php


If you are American, please don't be offended..it's a joke! ?
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzJan 02.2004 — np. sessions shouldn't disappear, so long as you dont send the user back to a url where the sessions get renamed, then it can screw up. thus u need an if statement to check if they are already set before you set them.
×

Success!

Help @tobyw_1969 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...