DrRobot Posted June 1, 2011 Share Posted June 1, 2011 Hello, I'm having an issue with using a php variable (of the current page url) as a cookies name. Here's an example of what I'm trying to do: $directory = "/main/index.php?id=7"; $cName = "path_".$directory; if(isset($_COOKIE[$cName])){ echo "Do something NOW!!!"; setcookie($cName, '1', mktime(23,59,59)); } echoing the isset check doesn't return anything, no 0, no 1. I can print_r $_COOKIE; and it shows the cookie exists, I just can't check if it exists with what I'm doing here. From what I've learned is that the cookie name is something like: "testing" it works just fine, but if it's that $cName variable it doesn't work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/ Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 so you've tried something like <?php if( isset( $_COOKIE[$cname] ) ) { var_dump($_COOKIE ); } else { echo "doesnt exist!"; } ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/#findComment-1223296 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 If you were developing with error reporting enabled, which you should be, you'd see a warning similar to: Warning: Cookie names can not contain any of the following '=,; \t\r\n\013\014' . . . Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/#findComment-1223297 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 Also, you echo immediately before setting a cookie. This is a no-no. cookies must be set before headers are sent Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/#findComment-1223304 Share on other sites More sharing options...
DrRobot Posted June 1, 2011 Author Share Posted June 1, 2011 If you were developing with error reporting enabled, which you should be, you'd see a warning similar to: Warning: Cookie names can not contain any of the following '=,; \t\r\n\013\014' . . . I guess this is probably the reason that the $_COOKIE[$cName] isn't working. Also, I have error_reporting(E_ALL); at the top of my file, I guess it's not working? BTW, I'm using WAMP. Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/#findComment-1223316 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 You probably need to add: ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/238051-php-cookie-issue-url-address-as-name-not-working/#findComment-1223324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.