cookies and if statement

I need help with this code. I need it to not loop every time I reload the page. Please take a look and let me know if there are any suggestions for fixing this issue. Thank you in advance.
[php]
if( !isset($_COOKIE[‘lang’]) ) {
setcookie( “lang”, “en”, time()+604800, “/”); /* expire in 1 week /
echo ‘no cookie’; #cookie isn’t set
}
else if( $_COOKIE[‘lang’] == ‘en’ ) {
setcookie( “lang”, “sp”, time()+604800, “/”); /
expire in 1 week /
} else {
setcookie( “lang”, “en”, time()+804800, “/”); /
expire in 1 week */
}

echo 'start ' . $_COOKIE['lang'];
echo '<br><br>';

echo 'finish ' . $_COOKIE['lang'];

exit;

[/php]

There is no loop in your code. But I do not understand why you are flipping languages in the else portion of the if structure.

Laffin,

I don’t know any better way to do it. Can you point me to a better solution? I want to be able to set a cookie that switches between the english or spanish, based on that I will show the information.

thank you,
jav

[code]

<?php $langs=array('en'=>'English','sp'=>'Spanish'); $lang=isset($_COOKIE['lang'])?$_COOKIE['lang']:'en'; // Set $Lang from Cookie or use en as default if(isset($_GET['lang'])) // Lang Button Pushed { $lang=($t=array_search($_GET['lang'],$langs))!==NULL?$t:'en'; // Check for valid lang, otherwise 'en' }

$strings=array(
‘en’=>array(
‘greeting’=>‘Hello’,
‘goodbye’=>‘Good Bye’),
‘sp’=>array(
‘greeting’=>‘Hola’,
‘goodbye’=>‘Adios’)
);

$langstrings=$strings[$lang];
?>

<?php echo $langstrings['greeting']; ?>

<?php echo $langstrings['goodbye']; ?>

[/code]

Laffin,

thank you for your fast response. this looks great! However, I will like to show the information using this method:
[php]<?php if($strings=‘en’){
showEnglishcopy();//just an example
}else if($strings == ‘sp’){
showSpanishcopy(); //this is just an example
}
?>[/php]
how can I get it to be this way?

the problem is that i’m showing whole content of page if the selected language is either or

Also, I will like the cookie to stick for 1 week.

[php]<?php
$english = setcookie( “lang”, “en”, time()+604800, “/”); /* expire in 1 week /
$spanish = setcookie( “lang”, “sp”, time()+604800, “/”); /
expire in 1 week */

$lang = array( $english, $spanish);

?>
[/php]

another thing I noticed with the code provided is that it adds the selected language in the address bar, which I really don’t want to do because I want it to be seamless and don’t want to show it in URL address bar test2.php?lang=English

please help! :frowning:

Laffin,

I have come up with a better solution. However, I still need help in separating the array. Please take a look and let me know what you think.
[php]
$array = array(1 =>‘esp’, 2 =>‘en’);
setcookie(“id”, implode(’,’ , $array));
if (in_array(‘esp’,$array)) echo “Sip”;
if (in_array(‘en’,$array)) echo “Yep”;
$store = serialize($array);[/php]

Thats because my examples should guide you to an answer. Usually these small scripts arent much more than to demonstrate a solution. From there you should be able to arrive at an answer. :slight_smile:
As I know a lot of this stuff, it does you no good, to get the answer and not learn from it. So simple scripts like above just demonstrate.

But if you want me to fix your code for you, I am willing at $40 an hr in increments of an hr :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service