Page 1 of 1
Why is my session getting lost?
Posted: Mon Nov 18, 2013 6:39 am
by simonmlewis
Code: Select all
if (isset($pageurl)) {
$pageurl = $pageurl;
$_SESSION['pageurl'] = $pageurl;
} else if (isset($_SESSION['pageurl'])) {
$url = $_SESSION['pageurl'];
}
I am using this to capture a particular session which is a URL.
Customer reaches a product page. URL is put in a session.
They click buy now, but because they are not logged in, they get asked to login or register.
If they click Register, they go to the registration page. By echoing $url on there, with the code above in the include file, it shows the URL is in that session.
Once they hit register it goes from /register (index.php?page=register) to login.php, which does all the DB checks, and logs them in. However, even if I strip out all that code, and just use the code above, and echo "$url";, it states:
[text]Notice: Undefined variable: url in C:\xampp\phpMyAdmin\site\login.php on line 11[/text]
Why does it store it in a session in the include files, but not in the *.php file??
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 6:43 am
by Celauran
$url isn't defined in the if block, only the else if block. You also have $pageurl = $pageurl, which I'm guessing is a typo.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 6:48 am
by simonmlewis
Oh yes, that is a typo because of a previous script.
Can I not use this:
I'm sure it will throw the odd error, but would this collate what's in the session??
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 6:53 am
by Celauran
So long as you're checking that the key exists first, absolutely you can use that.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 6:55 am
by simonmlewis
Code: Select all
if (isset($pageurl)) {
$_SESSION['pageurl'] = $pageurl;
} else if (isset($_SESSION['pageurl'])) {
$url = $_SESSION['pageurl'];}
echo "$url";
This is the same code as the Regiustration page, that echos the URL. Yet this on the login.php, doesn't.
ps i get the distinct impression I'm not handling sessions terribly well!
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:02 am
by Celauran
There's still no $url in your if block. You're only defining it in your else if block. If $pageurl is set, you'll get an undefined notice because you're trying to use $url without having defined it.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:08 am
by simonmlewis
Pardon my ignorance, but isn't $url being defined in the last bit? $url = $_SESSION['pageurl'];?
This page will always use $url. So I'm a bit lost now. Specially as the previous pages don't have this error, yet the codes are the same.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:09 am
by simonmlewis
Even if I do this:
Code: Select all
$url = NULL;
if (isset($_SESSION['pageurl'])) {
$url = $_SESSION['pageurl'];}
echo "$url";
It isn't showng the URL I have passed.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:26 am
by Celauran
simonmlewis wrote:Pardon my ignorance, but isn't $url being defined in the last bit? $url = $_SESSION['pageurl'];?
Only when the if condition isn't satisfied. When the if condition is satisfied, the else if block is skipped and $url remains undefined.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:26 am
by Celauran
simonmlewis wrote:Even if I do this:
Code: Select all
$url = NULL;
if (isset($_SESSION['pageurl'])) {
$url = $_SESSION['pageurl'];}
echo "$url";
It isn't showng the URL I have passed.
This would imply that $_SESSION['pageurl'] isn't set. Have you checked that?
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:30 am
by simonmlewis
Code: Select all
if (isset($pageurl)) {
$url = $_SESSION['pageurl'];}
echo "$url";
Like this?
... which also doesn't work.
Nor does this:
Code: Select all
if (isset($_SESSION['pageurl'])) {
$url = $_SESSION['pageurl'];}
else { $url = NULL;}
echo "$url";
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 7:52 am
by simonmlewis
Oh crap I see why. It's right there in my face (not something you'd have seen, but might have thought about).
Because it's passing from an include (which uses the index.php session_start()), and going to a login.php file, that file isn't using that session_start, so of course in that file there is NO SESSION.
Durrrr!!!!
What is the correct style of coding for it tho? There won't always be a "pageurl" in use.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 8:33 am
by Celauran
Insofar as coding it 'properly', your last example is fine. $url is always defined, so you won't get any undefined notices.
Re: Why is my session getting lost?
Posted: Mon Nov 18, 2013 8:36 am
by simonmlewis
Yeah I thought I was right with it - just made the rookie mistake at the start!
Re: Why is my session getting lost?
Posted: Tue Nov 19, 2013 4:57 am
by simonmlewis
Code: Select all
if (isset($_REQUEST['sorder'])) {
$sorder = $_REQUEST['sorder'];
$_SESSION['sorder'] = $_REQUEST['sorder'];
} else if (isset($_SESSION['sorder'])) {
$sorder = $_SESSION['sorder'];
}
if (empty($sorder))
{
$sorder = "title ASC";
}
The URL is:
http://site.local/seller/807&sorder=title%20DESC
If I use this url:
http://site.local/categ/49/BB-Guns&order=price%20DESC
With this code:
Code: Select all
if (isset($_REQUEST['order'])) {
$order = $_REQUEST['order'];
$_SESSION['order'] = $_REQUEST['order'];
} else if (isset($_SESSION['order'])) {
$order = $_SESSION['order'];
}
if (empty($order))
{
$order = "title ASC";
}
It works.
But on the "seller" page it doesn't. It always defaults to "title ASC".