Cookie Handling: is this correct all within one PHP file?

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:

Cookie Handling: is this correct all within one PHP file?

Post by simonmlewis »

Person visits web page.
It redirects them to the bottom of this page to view as Mobile (or normal).
They click mobile.

In theory the page turns over, triggers the $cookie to find it's mobile, and then further down it sees that Cookie, and directs them appropriately.

The issue here is, it's not taking them anywhere at all. It just turns the page over and shows the same thing. And if you then click View as Mobile again, it then gives you the wrong URL.

A few questions:
1) can you set a cookie at the top and then handle things within the same PHP file?
2) if yes to (1), why would then not turn the page over to the right redirect from "mobile"?

Code: Select all

<?php
$cookie = isset($_REQUEST['cookie']) ? $_REQUEST['cookie'] : null;
if (isset($cookie))
{
  if ($cookie == "mobile")
  {
  setcookie("site", "", time()-13600);
  setcookie("site", $cookie, time()+13600);
  }
  
  if ($cookie == "normal")
  {
  setcookie("site", "", time()-13600);
  setcookie("site", $cookie, time()+13600);
  }
}
$cookiesite = isset($_COOKIE['site']) ? $_COOKIE['site'] : null;

if(isset($cookiesite))
{
if ($cookiesite == "mobile")
{
$url = $_GET['url'];
$newstring = substr_replace($url, 'm', 7, 3);
echo "<meta http-equiv='Refresh' content='0 ;URL=$newstring'>";
}


if ($cookiesite == "normal")
{ 
$url = $_GET['url'];
$newstring = substr_replace($url, 'www', 7, 1);
echo "<meta http-equiv='Refresh' content='0 ;URL=$newstring'>";
}
}
elseif ($cookiesite == NULL)
{
$url = $_GET['url'];
$newstring = substr_replace($url, 'm', 7, 3);
echo "<br/><br/><div style='text-align: center'><a href='http://www.site.co.uk/ip_mobilehome.php?cookie=mobile&url=$newstring'>Choose Mobile Version</a><Br/><br/>
<a href='http://www.site.co.uk/ip_mobilehome.php?cookie=normal&url=$url'>Choose Normal Version</a></div>";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cookie Handling: is this correct all within one PHP file

Post by requinix »

simonmlewis wrote:1) can you set a cookie at the top and then handle things within the same PHP file?
In general. Cookies no: they are only sent when the browser requests a page, so if you set a cookie it won't be available until the next page.
If you're setting a cookie then also setting it in $_COOKIE, so the rest of the page can get the value, is fine. (I say this because it's normally bad practice to set things in the superglobal arrays.)

That may be enough to solve the problem but honestly I'm not entirely sure what behavior you're describing.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cookie Handling: is this correct all within one PHP file

Post by simonmlewis »

Is my page wrong then? So it won't set the Cookie (or reset it), and then run a query based on the content of that cookie using the method I've chosen?
I use to have a page called ip_cookieset.php, which ran it separately, but hoped it could all be run within the same page (less processing and less housekeeping).
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: Cookie Handling: is this correct all within one PHP file

Post by simonmlewis »

Code: Select all

<?php
$cookie = $_REQUEST['cookie'];


if ($cookie == "mobile")
{
$device = "mobile";
setcookie("site", $device, time()+13600);
}
if ($cookie == "normal")
{
$device = "normal";
setcookie("site", $device, time()+13600);
}

$cookiesite = $_COOKIE['site'];

echo "$cookiesite";

?>
This works, and produces "mobile" or "normal" depending on the url (?cookie=mobile...)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cookie Handling: is this correct all within one PHP file

Post by requinix »

It'll do that... on the next page load. If you clear your cookies and load the page one time you won't see any output.
Post Reply