If GET Variable Equals Nothing, Redirect?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

If GET Variable Equals Nothing, Redirect?

Post 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
Last edited by Benjamin on Tue May 26, 2009 4:26 pm, edited 1 time in total.
Reason: Changed code type from text to php.
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: If GET Variable Equals Nothing, Redirect?

Post 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' );
}
?>
 
:dubious:
Last edited by Benjamin on Tue May 26, 2009 4:28 pm, edited 1 time in total.
Reason: Changed code type from text to php.
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

Re: If GET Variable Equals Nothing, Redirect?

Post 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.
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: If GET Variable Equals Nothing, Redirect?

Post by mischievous »

Glad it worked for you! :drunk:
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: If GET Variable Equals Nothing, Redirect?

Post 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
 
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: If GET Variable Equals Nothing, Redirect?

Post 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! :dubious:
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: If GET Variable Equals Nothing, Redirect?

Post 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.
Post Reply