Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/98651-cant-get-my-cookies-to-work/
Share on other sites

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.

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...

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.