rykitz
rykitz

Reputation: 11

Keeping forward slashes in cookie PHP

I am building a log in page. Right now, there is an expiration on the page that I use a cookie for. I want the site to redirect back to the page they were on after they log in again.

For this, I use a cookie that has the name of the page stored in it (example, queue.php) and it will take them back.

This method works great if I'm going to a page that is in the same directory, but if I want to go back to a sub-directory, (for example, Deployment/deployment.php) it does not work.

I have tried urlencode(), backslashes, addslashes(), %2F in place of the forward slash, and nothing seems to be working.

This is how I am setting the cookie:

$url_name = "Deployment/deployment.php";
setcookie("url",$url_name);

Any help would be appreciated, I am completely lost.

Upvotes: 1

Views: 2331

Answers (5)

Lane
Lane

Reputation: 478

setrawcookie() seems to be the function you're going for. According to php.net, it is exactly the same as setcookie(), only it doesn't do any urlencoding.

Upvotes: 3

Rijk
Rijk

Reputation: 11301

What exactly doesn't work? As stated in the manual:

Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name.

So there's no need to manually encode/decode. I don't think the slash in the $url_name is the problem. I suspect the path you're redirecting to is somehow incorrect, but I can't check without more code.

Upvotes: 0

johnluetke
johnluetke

Reputation: 3483

Can you explain your problem a bit more? I just tried the following, and was able to retrieve the value of the cookie just fine:

<?php 
$url = "http://google.com/mail";
echo "Setting cookie with value " . $url . "</br>";
setcookie("url", $url);
echo "The cookie is: " . $_COOKIE['url'] . "</br>";
?>

Upvotes: 0

Devator
Devator

Reputation: 3904

I don't know where your login page is found, but if Deployment is a subdirectory (so one level beneath the main folder) you should add a forward slash infront of it, like so:

$url_name = "/Deployment/deployment.php";
setcookie("url", $url_name);

Upvotes: 1

pb149
pb149

Reputation: 2298

Perhaps you could base64_encode() the value and then base64_decode it upon retrieval? Just a though. I don't know why the forward slash wouldn't be allowed, though. Perhaps someone else will be able to enlighten me!

Upvotes: 0

Related Questions