Jump to content

how to redirect with php.


timgetback

Recommended Posts

Depends on what else you want to do..

Just remember that you cannot send any output to the browser prior to the header() and anything after won't be seen because its not on that page anymore.

As for it continuing execution of the script after the header() function, I am not sure if it does any of it.  I usually have exit() after my header() and then whatever else I need to do will be in the script that you're calling header with.

Link to comment
Share on other sites

i got another question i have this on my login page:

[code]if ((!$username) || (!password)) {
    include("error.html");
    exit;
}[/code]

but every time i type in a username and password it still brings me to the error page regarless of whether or not i type in the correct information.
Link to comment
Share on other sites

thanks... also when i added the header function at the end of my php file.... it kept telling me that i have an error at line 47  which is the line that has "?>" i dont understand why theres an error there.   When i moved the header to the top of my php file and added "exit;" right after it it repeated the same error.
Link to comment
Share on other sites

[quote author=timgetback link=topic=102866.msg408949#msg408949 date=1154620190]
thanks... also when i added the header function at the end of my php file.... it kept telling me that i have an error at line 47  which is the line that has "?>" i dont understand why theres an error there.  When i moved the header to the top of my php file and added "exit;" right after it it repeated the same error.
[/quote]

Might be you're missing a ending } somewhere in your code.
Link to comment
Share on other sites

im not familiar with java script... but heres my code:

[code]
<?php
session_start();
include 'db2.php';
$username = $_POST['username'];
$passwd = $_POST['password'];

$username = stripslashes($username);
$passwd = stripslashes($passwd);

if ((!$username) || (!$password)) {
    include("error.html");
    exit;
}
 
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (username, password)
        VALUES('$username', '$passwd')") or die (mysql_error());

$result = mysql_query($sql,$connection) or die (mysql_error());

header("location: http://www.domain.com");
    exit;
  ?>[/code]

I was thinking of adding "include 'redirect.php'; " and having the header there instead.....
Link to comment
Share on other sites

header location, for some reason doesn't always have to be at the top, there are a lot of times I use header location to change people over to a page based on them doing something.  I might ahve a bunch of scripts already ran, or running on the page, includes, and calculations, and everythin gelse, then in the middle of the page, have 1 header location based on if they do something wrong, it still seems to work no matter where you put the redirect at.
Link to comment
Share on other sites

[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

mail? :)
Link to comment
Share on other sites

[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

what???? so i cant have all my php scripts before the header function? Cause if you check i have it at the very end.. which casused problems....
Link to comment
Share on other sites

[quote author=onlyican link=topic=102866.msg409056#msg409056 date=1154627214]
if you have header somewhere other than the top
you need ob_start(); at the top of the page

[/quote]

If there is some output sent to the browser that is...
Link to comment
Share on other sites

[quote author=Ifa link=topic=102866.msg409045#msg409045 date=1154626925]
[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

mail? :)
[/quote]
Yes believe it or not mail does send headers.

[quote author=onlyican link=topic=102866.msg409056#msg409056 date=1154627214]
if you have header somewhere other than the top
you need ob_start(); at the top of the page

[/quote]
You'll want to add ob_start(); at the top of each PHP page and ob_end_flush(); at the end of each PHP page.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.