user1054606
user1054606

Reputation: 57

PHP skipping script and click anchor tag

I have a form. After the form is filled out and the user clicks the submit button the user is taken to a thank you page. On the thank you page is a link (anchor tag) for the user to get to her home page. It works fine 19 out of 20 times.

The problem is, sometimes php skips the thank you page and goes directly to the home page. How is this possible? How is php clicking the link? I've gone over the code and it's completely correct. There is no javascript, just html and css.

Like I said, it doesn't do it every time and I guess it's not a bid deal I'd just like to understand what's going on. I'm using a simple header redirect like so

$url = "thanks/";
header("Location: $url");
exit();

What do you guys think is going? Is there any way to stop it?

Thanks

Upvotes: 0

Views: 250

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26467

The RFC for the Location header requires a single absolute URI. This is also pointed out in the PHP manual in the notes section:

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

The problem may be to do with the fact that you're passing non standard headers to the browser. Browsers interpret the malformed header string differently and don't always behave as expected. Again as demonstrated in the PHP manual you should create an absolute URI, not an absolute or relative path before passing it to the header() function.

/* 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;

Upvotes: 1

Related Questions