Jump to content

problem with setting cookies for translating pages


dsaba

Recommended Posts

I am making a website that I want to be able to turn into different languages. English, Hebrew, Spanish, and Portueguese.

 

my brainstorms are this:

1. i create static html translated pages for each language

2. i pass lang=en/he/es through urls

3. a cookie is made for this variable that is passed through the url, so the page remembers the language that page needs to be translated in

4. if/else statements determine what the variable is if it is en, es, he, and then loads an include page which is that static html page this is translated

 

here is the code i was trying out: it does not translated to hebrew at all, only english, I don't know what I"m doing wrong, the basic steps of my process are listed above, if anyone has any better ideas on how to do this, please tell me

 

<?php
//setting cookie based on variable that has been pass by url
//the REQUEST command can retrieve variables from variables being pass by url
//like something.php?favmovie=starwars
setcookie("language_en", $_REQUEST['lang'], time()+3600,"/","");
setcookie("language_he", $_REQUEST['lang'], time()+3600,"/","");
$language_en = $_COOKIE['language_en'];
$language_he = $_COOKIE['language_he'];
//pagelanguage variable now is the main variable to use for if/else statements
//for english
if ($language_en == en)
{
include 'main_en.html';
}
else
{
$language_en = $_COOKIE['language_en'];
}
//for hebrew now
if ($language_he == he)
{
include 'main_he.html';
}
else
{
$language_he = $_COOKIE['language_he'];
}     
//also very important if the cookie is null maybe its their first visit
//then you need to make an if/else statement to display english page if cookie
//is null
if ($language_en = 'NULL')
{
include 'main_en.html';
}
else
{
echo 'there is no cookie available to make a variable from, and your if else statement to load an english page when that happens did not work';
}

?>

 

Link to comment
Share on other sites

i have new code that i wrote now

it doesnt work either, the regularpage.html displays for every url

whether its main.php?lang=en

or main.php?lang=he

 

still regularpage.html displays

this means that its supposedly read that the cookie was neither he or en

but apparantly i passed the variable correctly in my url like so: ?lang=en

 

so maybe something in my code is wrong? i appreciate the help

here is the link to the php page http://p2mhunt.awardspace.com/main.php

<?php
//setting cookie based on variable that has been pass by url
//the REQUEST command can retrieve variables from variables being pass by url
//like something.php?favmovie=starwars
$justsentlang = $_REQUEST['lang'];
setcookie("justsentlang", '$justsentlang', time()+3600,"/","");
$langcookie = $_COOKIE['justsentlang'];

if ($langcookie == "en") 
{
include 'main_en.html';
}

if ($langcookie == "he")
{
include 'main_he.html';
}
else
{
include 'regularpage.html';
}
?>

Link to comment
Share on other sites

ok i have a new piece of code

it somewhat works, except something weird happens

it displays the content that is supposed to be dislayed when the cookie is read correctly

BUT it also displays the content when the cookie is supposed to be NULL

so what is going on? how can it be displaying both?

please help i have taken time to explain and repost different pieces of codes that I made myself from scratch, it may be beginners, but at least i made it myself-thank u

<?php
//setting cookie based on variable that has been pass by url
//the REQUEST command can retrieve variables from variables being pass by url
//like something.php?favmovie=starwars
setcookie("language", $_REQUEST['lang'], time()+3600,"/","");
$langcookie = $_COOKIE['language'];


if ($langcookie == "en") 
{
include 'main_en.html';
}

if ($langcookie == "he")
{
include 'main_he.html';
}

if ($language_en = 'NULL')
{
include 'regularpage.html';
}

//see if it even retrieves the cookie right
if ($langcookie == "en")
{
echo 'the cookie is read as en';
}

if ($langcookie == "he")
{
echo 'the cookie is read as he';
}
?>

Link to comment
Share on other sites

Try changing to this and see what happens:

 

<?php
//setting cookie based on variable that has been pass by url
//the REQUEST command can retrieve variables from variables being pass by url
//like something.php?favmovie=starwars
setcookie("language", $_REQUEST['lang'], time()+3600,"/","");
$langcookie = $_COOKIE['language'];


if ($langcookie == "en") 
{
include 'main_en.html';
}

elseif ($langcookie == "he")
{
include 'main_he.html';
}

elseif ($language_en = 'NULL')
{
include 'regularpage.html';
}

//see if it even retrieves the cookie right
if ($langcookie == "en")
{
echo 'the cookie is read as en';
}

if ($langcookie == "he")
{
echo 'the cookie is read as he';
}
?>

Link to comment
Share on other sites

thank you very much for your reply, i'm sure you hear it all the time from helping people

but I really do appreciate it :)

 

this works, however you still have to click on the "change to hebrew" or "change to english"

link twice for it to really change

 

basically you have to click on the link twice for the page to load? anyone know why?

this happens even when you open it in a new window

 

try it: http://p2mhunt.awardspace.com/main.php

Link to comment
Share on other sites

I get errors on your page:

 

Warning: main(main_es.html): failed to open stream: No such file or directory in /home/www/p2mhunt.awardspace.com/main.php on line 59

 

Warning: main(): Failed opening 'main_es.html' for inclusion (include_path='.:/usr/local/php4/share/pear') in /home/www/p2mhunt.awardspace.com/main.php on line 59

Link to comment
Share on other sites

i think the problem is because I'm setting or making a cookie every single time the page is access

and this deletes the old existing cookie

 

i need to make an if statement to check if such a cookie already exists

if so then display

if not then set a cookie

 

is there a way to make an if statement to check if a cookie exists?

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.