Page 1 of 1

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

Posted: Mon Nov 04, 2013 11:12 am
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>";
}

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

Posted: Mon Nov 04, 2013 4:17 pm
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.

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

Posted: Mon Nov 04, 2013 4:22 pm
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).

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

Posted: Mon Nov 04, 2013 4:46 pm
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...)

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

Posted: Mon Nov 04, 2013 5:17 pm
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.