note 32914 deleted from function.header by sniper
| From: | sniper@php.net | Date: | Sun, 07 Sep 2003 21:46:18 +0000 |
| Subject: | note 32914 deleted from function.header by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55653@lists.php.net to get a copy of this message | ||
Note Submitter: sahasrahla@hotmail.com
----
I have a header() redirect that I wanted to base off of the existance or validity of a variable.
This particular page HAS to have a $var="something" variable (pulled from the query string
of the URL) to render properly and it redirects to the homepage of my site if $var is missing or
invalid.
First it checks to see if $var exists or is a zero-length string. If $var does exist and has
content, I check it against the MySQL database to make sure it's valid.
The problem I was running into here was that mysql_query() and/or mysql_num_rows() was returning
something to the browser which would cause the header() rediect to fail. My best guess is that those
functions keep a print ""; command open for returning errors, and just print an empty
field if there is no error. Nonetheless, it was enough to kill the header() call.
So when I ran the mysql_query() and the mysql_num_rows() I suppressed the output with an @ in front.
This allowed the second header() call in the code to function properly.
Here's the whole code:
if(!isset($var) || $var == ""){header("Location: http://www.mysite.com");}
else{
$request = @mysql_query("SELECT * from page_data WHERE directory = '$var' LIMIT
1");
if(@mysql_num_rows($request)!=1){header("Location: http://www.mysite.com");}
else{list($age,$height,$weight)=mysql_fetch_row($request);}
}
So with this code in place, if I went to:
http://www.mysite.com/thispage.php
http://www.mysite.com/thispage.php?var=
or
http://www.mysite.com/thispage.php?var=invalid_data
They would all redirect me to my site's root. Otherwise (if the $var is matched in the db) it
pulls the variables $age, $height and $weight and continues loading /thispage.php