Jump to content

not retrieving $_GET correctly


cedricganon

Recommended Posts

Hello everyone. I had another account here but it seems to have been wiped from the face of the internets. oh well. I was working on a basic form on a multilanguage website so i need it to properly acknowledge what language it is working with. the link to the page would be like so:

 

index.php?lang=en&sb=1

 

so the code, as required to read it, is as follows:

 

if(isset($_GET['lang']) == TRUE){
switch($_GET['lang']){
	case 'en':
	case 'it':
	case 'pl':
		$validlang = $_GET['lang'];
		break;
	default:
		$validlang = 'en';
		break;
}
}
else{
$validlang = 0;
}
if(isset($_GET['sb']) == TRUE){
if(is_numeric($_GET['sb'] == FALSE)){
	$error = 1;
}
if($error == 1){
	header("Location" . $config_basedir);
}
else{
	$validentry = $_GET['sb'];
}
}

if($validlang == 0){
echo "An unknown error has occurred." . mysql_error();
}

 

for whatever reason $validlang always comes out with the value 0. I even set tried setting $validlang = 'en'; before that snippet to at least force it to hold some value but I'm obviously doing something stupid here (isn't that usually the case  ::)) that I can't seem to catch. Can anyone help clear this up for me? I really appreciate it!

Link to comment
https://forums.phpfreaks.com/topic/250342-not-retrieving-_get-correctly/
Share on other sites

You have your logic wrong..

 

Your script does the following...

- First checks to see whether there is a query string..

- If true, the switch checks each possibility.

        Inside this switch, you have empty cases with no breaks..  you also have a default case..

- If there isn't a query string, $validlang is set to zero when it should have gone through the switch to get the default case.

 

You need to first check for the querystring... set a flag or error or whatever.. THEN exit out of that if into the switch.

What I meant was, that currently, the switch statement only executes if $_GET['lang'] is set... and inside that switch you have the desired default value for cases when $_GET['lang'] doesn't exist. 

 

Metaphor time,... imagine you're in a hallway of doors.. You say to yourself, if (and ONLY if) I enter room 35, I will take a dump.

Then when you enter room 35, you go to the bathroom and before dropping your load you say, "Well, if I entered room 36 I would have also taken a dump" ...  What's the point of saying that since you're already in room 35?

 

That's what you're doing with your isset right now..

You say, well, if there is a lang variable attached, then I will do this with it, but I will also make a case in case there is no lang..

Then directly after that, you do the same thing again..  but instead you assign it a value of zero.

Archived

This topic is now archived and is closed to further replies.

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