Redirect PHP after script is run
I have a script that submits emails entered into a form to a database and I am wondering if there is a way to redirect the PHP script after the email has been submitted?
my script is -
<?php
$con = mysql_connect("localhost","User","Pass")开发者_JS百科;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
please could someone give clear instructions as I am learning PHP still.
To take this redirect one step further. The header "location" should be used with a 302 response code instead of a classical 200 response code. Thios response code is 'global' to the HTTP response. 302 means 'redirect'. Most browser handle the location header even without the 302 response code, but you should set it (it could help for example ajax handling of your response).
header("Location: /foo.php",TRUE,302);
Now one step more. In fact a 303 response code (see-other) is a better redirect response code. It's called as well the redirect after post. This special redirect means for your browser that it's not really a redirect because the page he asked whas in the wrong place, but that it's really a redirect because after your POST action you need a new page result.
header("Location: /foo.php",TRUE,303);
Edit: as stated by thirtydot, a better usage is absolute url in the redirect
header("Location: http://www.example.com/foo.php",TRUE,303);
Remember that POST implies potential data changes (that GET request should'nt imply). A POST followed by a 303 is the right way to go, and will prevent your BACK button reposting the same request.
Use:
$url = 'http://your.redirect.url';
header('Location: ' . $url);
Try this:
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email) VALUES ('$_POST[email]'')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
mysql_close($con);
header('Location: http://www.example.com/');
This code taken from the header()
manual page will redirect to another page on the same domain and in the same directory:
/* 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;
header("Location http://www.example.com");
That will redirect the browser to example.com, note that you can only use this if output has not already been sent to the browser, e.g make sure you have echo
, print
or var_dump
statements before you use header()
Add send header function
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
mysql_close($con)
?>
Or send javascripts with code
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
echo "<script>";
echo "window.location.href= '".$_SERVER["HTTP_REFERER"]."';";
echo "</script>";
}
mysql_close($con)
?>
Redirect in the controller.
header('Location: http://www.yoursite.com/new_page.html');
精彩评论