0

i have the following function

function redirect_to( $location = NULL ) {
  if ($location != NULL) {
   header("Location: {$location}");
    exit;
  }

then I use it to redirect with an argument

$id = $photo->id; // integer 
redirect_to("photovietnam.php?id=$id");

This work fine on my local system with wamp. on the server the correct url is displayed in the head ,but it appears to lack a return

did anybody had the sam experiance ?

2
  • 1
    What do you means by it appears to lack a return ?? Commented Feb 3, 2014 at 11:55
  • 1
    Also note, that you can only redirect using the header() method if NO output has happened. Once you echo something, the headers are send, and you can not manipulate them again. Commented Feb 3, 2014 at 11:58

1 Answer 1

1

Check the docs: http://de3.php.net/manual/en/function.header.php, you might need to have an absolute path to the resource.

Note:

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add some sense of completion: always check if any output has been sent to the browser before calling header(). This includes server side errors that depend on various host configuration. If the machine you're deploying on sends error messages before you send new headers, there might be your issue.
just following up on @VictorNitu's suggestion - you can always to an error_reporting(0) at the start of your page, to get rid of those errors. Your function looks fine, but is there any code above the line where you are calling the function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.