tags:

views:

278

answers:

7
+2  Q: 

php page redirect

can php make a redirect call after executing a function? i am creating a function on the completion of which i want it to redirect to a file located in the same root folder. can it be done?

 if {
      //i am using echo here
 }

 else if ($_SESSION['qnum'] > 10) { 
            session_destroy();
            echo "Some error occured.";
            //redirect to user.php
        }
A: 

The header() function does this:

header("Location: user.php");
flurin
Please use absolute URLs according to the HTTP specification.
Gumbo
True, but make sure you remove the echo first or you might experience a "Headers allready send" ;)
D4V360
A: 

Yes.

In essence, as long as nothing is output, you can do whatever you want (kill a session, remove user cookies, calculate Pi to 'n' digits, etc.) prior to issuing a location header.

middaparka
A: 

Use http_redirect

GreenMatt
this is a pecl function, isn't it? you first have to install the right pecl extension.
flurin
@flurin: Yes, it's a PECL function, but I didn't think that would be a problem.
GreenMatt
@GreeMatt yeah you're right that would not be a problem. just wanted to mention for all the php-learners out there that they would first have to install something...
flurin
@flurin: A good thought, thanks.
GreenMatt
+1  A: 
header( "Location: http://www.domain.com/user.php" );

But you can't first do an echo, and then redirect.

code-zoop
please see the edit.
amit
+8  A: 

Yes, you would use the header function.

header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();

It is a good practice to call exit() right afterwords so that code below it does not get executed.

Also, from the documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

This means you should not echo anything right before the header() function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.

bkildow
You should also set the HTTP Status to 302.
Allain Lalonde
"The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set."
colithium
A: 

Using a javascript as a failsafe will ensure the user is redirected (even if the headers have already been sent). Here you go:

//$url should be an absolute url
function redirect($url){
    if (headers_sent()){
      die('<script type="text/javascript">window.location=\'', $url ,'\';</script>');
    }else{
      header('Location: ' . $url);
      die();
    }    
}

If you need to properly handle relative paths, I've written a function for that (but that's outside the scope of the question).

brianreavis
this doesnt actually work.
amit
how descriptive.
brianreavis
Change this line to this and it will... `die('<script type="text/javascript">window.location=\''.$url.'\';</script>');`
gmcalab
A: 

I would probably set the message as a session variable, redirect the user to another page which displays the message and destroy the session.

Leprosy