Why is my session getting lost?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Why is my session getting lost?

Post 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??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post by simonmlewis »

Oh yes, that is a typo because of a previous script.

Can I not use this:

Code: Select all

$url = $_SESSION['pageurl'];
I'm sure it will throw the odd error, but would this collate what's in the session??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post by Celauran »

So long as you're checking that the key exists first, absolutely you can use that.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my session getting lost?

Post by Celauran »

Insofar as coding it 'properly', your last example is fine. $url is always defined, so you won't get any undefined notices.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post by simonmlewis »

Yeah I thought I was right with it - just made the rookie mistake at the start!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my session getting lost?

Post 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".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply