note 32914 added to function.header
| From: | sahasrahla at hotmail dot com | Date: | Tue, 10 Jun 2003 21:20:20 +0000 |
| Subject: | note 32914 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-50127@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32914
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32914&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32914&report=yes