Reputation: 278
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 .
Upvotes: 0
Views: 135
Reputation: 1070
<?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.
Upvotes: 0
Reputation: 1250
if($_SERVER['REQUEST_URI'] === "/page/1/blog"){
// do code here
}
Request URI will give you the current URL after the domain name.
Upvotes: 2