Jump to content

[SOLVED] Cookies


Doctor_Strop

Recommended Posts

Hi all,

 

I'm a noob with PHP, but I've done a fair amount of reading. I am trying to set and get cookies on my site so I can alter the language, and the user will still have the language set when they surf back in. I can manage to set the cookie in a file as follows...

 

<?php

$language = $_GET['language'];

$refPage = "http://bfhq.co.uk/";

 

$name ="BFHQlanguageselect";

$expiry = 60 * 60 * 24 * 180 + time();

$path = "../languages/".$language;

$domain = "bfhq.co.uk";

 

if (is_dir($path)) {

if (opendir($path)) {

setcookie($name,$language,$expiry);

if (isset($_COOKIE[$name])) {

header('Location: '. $refPage);

}

}

}

?>

 

but when I try and call it again nothing happens and no cookies are found. See my code below for calling the cookie...

 

<?php

$name ="BFHQlanguageselect";

 

//check cookie even exists

print_r($_COOKIE);

 

if (isset($_COOKIE[$name])) {

$language = $_COOKIE[$name];

} else {

$language = "english";

}

?>

This is doing my head in as from what I have read this should work. My server is set up to read PHP 4 and 5.

 

Regards

Andy >:( >:(>:(

Link to comment
Share on other sites

If this is in the same script, you won't be able to read the cookie until the next time the page is reloaded.  So if you try to set a cookie and then read it again, you won't get a result.  If that is indeed your problem, let me know.  I have a workaround for it.

Link to comment
Share on other sites

Try this:

 

<?php

if (isset($_COOKIE['BFHQlanguageselect'])) {
   $language = $_COOKIE['BFHQlanguageselect'];
} else {
   $language = "english";
}

 

I did that just in case your $name variable was out of scope.

 

I tried that and unfortunately it still does not show any cookie. It's almost as if the cookie is getting deleted as soon as the script opens the home page.

Link to comment
Share on other sites

If this is in the same script, you won't be able to read the cookie until the next time the page is reloaded.  So if you try to set a cookie and then read it again, you won't get a result.  If that is indeed your problem, let me know.  I have a workaround for it.

 

Hi mate, both are in different scripts. The first script is linked to rather than included. The second script that finds the cookie (supposedly) is included. Interestingly enough, if any other cookies are present it finds them with the following command... print_r($_COOKIE);

 

Link to comment
Share on other sites

It still doesn't work. I tried refreshing.

 

I think the cookies are being created. My computer is accepting cookies because I have other cookies for other sites.

 

Could it be something to do with the expiry, as I reckon either the cookie is being deleted as soon as I leave the page via...

 

    if (isset($_COOKIE[$name])) {

        header('Location: '. $refPage);

      }

 

which proves the cookie existed.

Link to comment
Share on other sites

Try setting an explicit expiration time in seconds for the cookie.  Also, instead of doing the header thing, try just making a simple page first that just says

 

print "I found the cookie."; 

 

or something like that.  You could narrow down the problem.

Link to comment
Share on other sites

:D :D :D

ABSOLUTELY massive thanks. Did what you said and took all the header bits out. In the end it was because I had not set the "path" as "/", and the "domain" as bfhq.co.uk, so the call to access the cookie was over looking it. It now works perfectly.

 

setcookie(name, value, expire, path, domain);

 

regards

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.