0

I am using WordPress to set a cookie for a specified page , but it can't be done by adding the php code on the page as with PHP, cookies are set in the web page header lines, before any page content is processed.

I found a solution and it's to edit the index.php file and adding the code in the top , but this will add the code for the all pages .

So i want a php code that will get the webpage url for example

if ($pageUrl == 'http://website.com/another_page')
 {
setcookie("cookie[one]","cookieone" , time()+3600*720); 
if (isset($_COOKIE["cookie"]))  
{
header("Location: http://website.com/page");
  } 
}

PS: The code above maybe broken .

user00239123
  • 270
  • 4
  • 16

2 Answers2

2
if($_SERVER['REQUEST_URI'] === "/page/1/blog"){
  // do code here
}

Request URI will give you the current URL after the domain name.

Justin E
  • 1,252
  • 16
  • 30
0
<?php

    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $message = ($actual_link == "http://localhost/SO/URI/") ? "Works" : "Doesn't work";
    echo $message;
?>

Works just fine when I test it locally.

CmdrSharp
  • 1,085
  • 13
  • 30
  • You got a small problem with your code I tested it but it didn't work . it worked when I added / before [REQUEST_URI] – user00239123 Jun 24 '14 at 19:10