clown[NOR] Posted March 30, 2008 Share Posted March 30, 2008 i've been reading page up and page down about this thingy.. but it just wont work... what i'm trying to do is to set a cookie named language that controlls what language file that should be included.. and when i try to echo the cookie in the same file that holds the setcookie thingy it outputs the right value. but when i try to output the cookie on the index.php nothing happens so i tried using print_r($_COOKIE) and all i got back then was Array ( [phpSESSID] => oia8qdhnr5qd7pgm0tca9s6gl1 )[/quote] here's the following codes i use:set_lang.php <?php $newLang=$_GET['lang']; $newLangPath="../language/$newLang.php"; if (!file_exists($newLangPath)) { $newLang="english"; } setcookie("language","",time()-31556926); setcookie("language",$newLang,time()+31556926); header("Location: ../index.php"); ?> functions.php <?php function chkLang() { if (!$_COOKIE['language']) { return "english"; } elseif ($_COOKIE['language']) { return $_COOKIE['language']; } } ?> index.php <?php require_once( 'includes/functions.php' ); $lang=chkLang(); $useLang="language/$lang.php"; if (!file_exists($useLang)) { $useLang = "language/english.php"; } require_once( $useLang ); print_r($_COOKIE); ?> does anyone see something that i'm not? i havent worked to much with cookies, but i cant find any flaws anywhere. Thanks in advance - Clown Quote Link to comment https://forums.phpfreaks.com/topic/98651-cant-get-my-cookies-to-work/ Share on other sites More sharing options...
wildteen88 Posted March 30, 2008 Share Posted March 30, 2008 You cannot use cookies as soon as they are set, eg: setcookie('cookiename', 'cookie_value', time()+3306); echo $_COOKIE['cookiename']; You'll notice no value is display when you run that code on the same page. If you refresh the page the cookie value will be shown. This is due to the way the http protocol works. When PHP see's the setcookie function, the server will send a cookie header which will tell the browser to set the cookie you setup in the script. However the browser will not send the cookie data back to server after setting the cookie. Web browsers will only send cookie data per http request, so when you refresh the page the browser will re-request the current page again and thus this time send the new cookie data in the header. You'll then be able to retrieve the 'cookiename' cookie in your script. Quote Link to comment https://forums.phpfreaks.com/topic/98651-cant-get-my-cookies-to-work/#findComment-504875 Share on other sites More sharing options...
clown[NOR] Posted March 30, 2008 Author Share Posted March 30, 2008 well that's weird .. cuz when i do this <?php $newLang=$_GET['lang']; $newLangPath="../language/$newLang.php"; if (!file_exists($newLangPath)) { $newLang="english"; } setcookie("language","",time()-31556926); setcookie("language",$newLang,time()+31556926); echo $_COOKIE["language"]; // header("Location: ../index.php"); ?> it returns the right value, but when I remove the // and it redirects me to the index.php i get nothing ... doesnt even say that the cookie named "language" is set... Quote Link to comment https://forums.phpfreaks.com/topic/98651-cant-get-my-cookies-to-work/#findComment-504883 Share on other sites More sharing options...
clown[NOR] Posted March 30, 2008 Author Share Posted March 30, 2008 nothing? Quote Link to comment https://forums.phpfreaks.com/topic/98651-cant-get-my-cookies-to-work/#findComment-504934 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.