Reputation: 4173
Its a very silly problem but somehow it is not working I have a function to create a file, if it goes through with it i want it to redirect the user to X page.. in this case 1.php.... but somehow is not working :S why?
//Creates File, populates it and redirects the user.
if (createfile($dbFile)) {
header('Location: 1.php');
}
Upvotes: 0
Views: 90
Reputation: 15903
You need to exit()
after sending a redirect header:
if (createfile($dbFile)) {
header('Location: http://yoursite.com/path/to/1.php', true, 302);
exit();
}
Otherwise, PHP continues to execute. If you exit()
, the client receives the header right after you make the call to header()
.
You should also heed this warning on the PHP docs page for header()
:
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']
anddirname()
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;
?>
Upvotes: 4
Reputation: 21396
Try;
if (createfile($dbFile)) {
header("Refresh: 0; URL=1.php");
exit();
}
Upvotes: 1
Reputation: 2683
I had a similar sounding problem where code was still being executed after the header location. That's why I always do exit(); afterwards
Upvotes: 0