Page 1 of 1
If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:21 pm
by azylka
If a GET variable is equal to nothing, can I somehow redirect the user to a different page?
Is this possible?
PHP Code:
Code: Select all
<?php
$login = $_GET['status'];
if $login = ""
header( 'Location: ../login.php' ) ;
?>
I'm very new to PHP, and people on other forums won't help me. Can anybody review this, and then edit it? I know that "" returns false, and that I should have {}'s but I just need help, and quickly.
Thanks,
Alex
Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:27 pm
by mischievous
Code: Select all
<?php
$login = $_GET['status'];
if ($login == "")
{
header( 'Location: ../login.php' );
}
?>
or you could do
Code: Select all
<?php
if (!isset($_GET['status']))
{
header( 'Location: ../login.php' );
}
?>

Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:36 pm
by azylka
Thanks for your speedy reply! I have yet to test this, but thanks for that anyway.
Thanks,
Alex
EDIT: Just tested this! Thanks SO much.
Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:51 pm
by mischievous
Glad it worked for you!

Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:52 pm
by Zoxive
Just an FYI there is a difference between "nothing" and isset.
http://php.net/isset
Ex:
Code: Select all
<?php
$myvar = '';
var_dump(isset($myvar)); // returns true. Meaning it is set
var_dump(empty($myvar)); // returns true. Meaning $myvar is empty
Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 4:59 pm
by mischievous
Zoxive, good tip! I believe he was using it for some type of login system with the get variables
(ie: domain.com/index.php?status=loggedin)
So the only way that the variable status would be established is if the url was stated as so. That's why I ran with the isset??? I Think that's correct to say...
In your example you had a variable set in scripting $myvar = ''; which would always be there...
Just my 2 cents on that!

Re: If GET Variable Equals Nothing, Redirect?
Posted: Tue May 26, 2009 5:03 pm
by Zoxive
My post was an example to extinguish the difference between empty and isset. Down the road if he kept using isset for all his "nothing" checks he could get confused to the behavior of isset.