Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirecting to url

Status
Not open for further replies.

rcubed

IS-IT--Management
Joined
Feb 10, 2004
Messages
7
Location
US
I have a form that prompts for username and password and does a mysql query to verify and pulls a url from a field in the database and then I want to send the logged in person to this url. Itried this but does not seem to work:

header (location: "$url")

What is wrong with this? Please help
Thanks
 
This will not work as the header() function has to be called before anything else is output to the browser.

I don't believe you can use a variable with it because of this.

If you are trying to do a redirect try using a META tag in the <HEAD> of the html.

ie
Code:
echo &quot;<meta http-equiv=\&quot;refresh\&quot; content=\&quot;0; url=/mypage.php\&quot;>&quot;;

Remember though this needs to be encapsulated in the <HEAD> tag and this may not work on all browsers.

To use this method you need to do your processing before the <HTML> and <HEAD> tags are sent to the browser.

D
 
damonac,

I think this script is called from a canlogin.php or something else, where the only script is check in DB if user exists, if so get his personal url and send him there. No &quot;echo&quot; before, just mysql_query and header.

In addition.. this is a copy/paste from php.net:
<?php
header(&quot;Location: . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. &quot;/&quot; . $relative_url);
?>

as you can see, you can use variables with headers.

Hope this helps.
 
Using the header() directive is the best option. As danomac says, it has to be the first output to the browser.
So, the solution is: don't put anything into the output stream before calling header().
You can do whatever PHP code before that, as long as no output is generated. You can actually use output buffering - if you are worried about anything getting into the stream - to prevent any output.

What is wrong?
Code:
header (location: &quot;$url&quot;)
The syntax is wrong the way presented. It should be:
Code:
header(&quot;Location: &quot;.$url);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top