Welcome to WebmasterWorld Guest from 3.92.92.168

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Header Redirects and Cookies.

     
11:02 am on Jan 18, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Hi peeps,
I need some help with a language recognition script that I've written...

The page works fine until you disable cookies. I wrote a script to compensate for no cookies but that caused the website to exhibit some really unusual behaviour, like not displaying their language first time around... (I know why this was.)

Here's my code - the question will follow:

// includes/assign_language.php
// THIS FILE APPEARS IN THE HEADER OF EVERY PAGE
// THIS PAGE ASSIGNS A LANGUAGE
// THIS PAGE HAS NOTHING TO DO WITH THE CONTENT THE USER SEES - ONLY THE LANGUAGE THEY SEE IT IN

// AND ONE ISN'T SET, SET ONE
if(isset($_COOKIE['language_preference']) == FALSE)
{
// SET COOKIE EXPIRY TIME
$number_of_days = 1825; // APPROX. 5 YEARS
$date_of_expiry = time() + 60 * 60 * 24 * $number_of_days;

Set their language cookie here...

// REFRESH THE PAGE TO UPDATE THEIR LANGUAGE
header('Location: http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
}

// SET THE LANGUAGE ID FROM THE COOKIE
$languageid = $_COOKIE['language_preference'];


Now my question is...

How can I stop the infinite loop of page refreshes when the user doesn't have cookies?

I basically need to refresh the page once and if the cookie still isn't set, stop the loop.

Any help would be awesome!

Thanks in advance,
Tom.
11:13 am on Jan 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 0


Hi there Tom_Cash,

When in the loop run a if clause to say if $_COOKIE ! set break; that will then exit the loop - though there may be other alternatives - Ha! If this were VB using a goto would be advisable; having said that you can do that in php but I don't want to over complicate things at this stage.

How are you handling this it the user doesn't have cookies? I'm curious now :)

Cheers,
MRb
12:21 pm on Jan 18, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Heya mate,
I tried that and it never worked. I then added = (as in !=) and I just got the following error.

Fatal error: Cannot break/continue 1 level in assign_language.php on line 65

This was the code:

if ($_COOKIE != set)
break;

header('Location: http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);


If the user doesn't have cookies it will merely show their native language and present them with no translation options.
3:57 am on Jan 19, 2011 (gmt 0)

New User

5+ Year Member

joined:Jan 19, 2011
posts:28
votes: 0


Try
If(!COOKIE) {
die('Please turn on cookies');
}

I'm not entirely sure, but it should in theory work, as long as the if is correct.
12:14 pm on Jan 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Thanks for the reply, Shingetsu.

However, it didn't work for me mate. I've tried other variations of this too, but to no avail.
12:38 pm on Jan 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 0


Hi there Tom_cash,

Sorry for the delay in responding - MEGA busy atm. Could you post the code that's relevant and we shall see if we can work if out from there, logic to this is simple enough.

Cheers,
MRb
5:59 pm on Jan 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


if ($_COOKIE != set)
break;


Break is only valid inside a for/foreach/switch or other looping construct, sounds like that code was used outside one.

Don't go down the road of instructing your users to modify their behavior to suit your environment (it can be done, I just won't show you how ATM. :-) ) Consider for a moment, maybe they are on a system, or using a browser, that *CAN'T* enable cookies.

A better solution is to fall back to query strings. In any PHP session, there should be a PHPSESSID cookie.

if (isset($_COOKIE['PHPSESSID'])) {
// do your cookie logic
}
else {
// output ALL URL's dynamically, and append every URL with ?language=selected language
}

Then in all functions requiring a read-in of the selected language, you read for cookie first, if not found, query string.

Yikes. I know. This means a lot of work. But accessibility generally is. :-)

A side note, there may be a way to port this all over to $_SESSION variables that will work. The reason I say this is:

- See above. PHP Sessions keep their connection to the client with the PHPSESSID cookie.

- PHP *inherently* does exactly what I've mentioned; if cookies are not enabled, it will output query strings for PHPSESSID like SID=dsf4353fdsfgds66787

- So, if you move your $_COOKIES into $_SESSIONS and cookies are disabled, PHP may do all the work for you automagically.

It's worth a shot and would take less time to experiment with.
6:23 pm on Jan 19, 2011 (gmt 0)

New User

5+ Year Member

joined:Jan 19, 2011
posts:28
votes: 0


hum, try setting a cookie:
$_COOKIE['checker'] = 1

then do a small if
if($_COOKIE['checker']!=1) {
die('Error, please enable cookies')
}
2:30 am on Jan 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


And if they *can't* enable cookies?
3:42 am on Jan 20, 2011 (gmt 0)

New User

5+ Year Member

joined:Jan 19, 2011
posts:28
votes: 0



Function Sessionc()
{
$_SESSION['test'] = 1;
if($_SESSION['test']=1) {
echo "please turn on browser cookies. Meanwhile, we will use your session";
$SESSION['use'] = 1;
} else {
die('Error, cookies have been disabled and session is not responding. To fix please activate cookies');
}
}
$_COOKIE['test'] = 1;
if($_COOKIE['test']!=1) {
Sessionc;
}

then later...

If($_SESSION['use']=1) {
INCLUDE "path to session code file";
} else {
INCLUDE "path to regular cookie file";
}
8:46 am on Jan 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 0


Hi all,

if (isset($_COOKIE['PHPSESSID'])) {
// do your cookie logic
}
else {
// output ALL URL's dynamically, and append every URL with ?language=selected language
}


Yup, that looks about best to me, PHPSESSID is a very useful thing to have, it's the way that php maintains $_SESSION's from the browser.

I misunderstood the original question, and yes: My Bad, Break; is within a loop - as it's name suggests so that wouldn't have achieved anything. I shall get better glasses I think :)

If your going to do $_SESSION checks, check that they are set first before you check the value, else you will get undefined variable error's:-

if(isset($_SESSION['test']) && $_SESSION['test'] == "your value"){
//run checks
}
else{
//run other checks
}

Cheers,
MRb
10:05 am on Feb 22, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Wow, sorry it's been such a long time before I expressed any sort of thanks!

I really appreciate all this input! I finally got chance to give it a bash this morning and $_COOKIE['PHPSESSID'] works a treat.

Thanks guys - can't express it enough!