将PHP页面重定向到另一个PHP页面

时间:2010-04-30 02:00:15

标签: php

我有一个register.php,login.php和main.php。如何在注册提交到登录页面后重定向用户,然后登录页面提交到主页面。

4 个答案:

答案 0 :(得分:7)

header("Location: /login.php");
exit; 

请参阅exit。不要忘记这样做。如果您在此之后有敏感数据,那么任何不遵循位置标题的人都可以看到它(such as some bots)。

为了阻止您忘记,您可以构建一个包装类型函数

function redirect($url) {
    header('Location: ' . $url);
    exit;
}

答案 1 :(得分:3)

header("Location: /login.php");

http://php.net/manual/en/function.header.php

答案 2 :(得分:0)

你可以这样做:

<强> func.php

<?php
function EmptyStr($value){
   $str = strval($value);
   $str = str_replace("&nbsp;", "", $str);
   return (trim($str) == "");
}

function redirect($url) {
header('Location: ' . $url);
exit;
}
?>

<强> register.php

<?php
include("func.php");
$username = $_POST["username];
$password = $_POST["password"];
$email = $_POST["email"];
if(!EmptyStr($username) && !EmptyStr($password) && !EmptyStr($email)){
   sql = WRITE YOUR SQL SYNTAX HERE TO INSERT THE INPUT TO DB
   redirect("/login.php");
}else{
   $_SESSION["ErrMsg"] = "Error! All fields are required.";
}

echo $_SESSION["ErrMsg"];
$_SESSION["ErrMsg"] = "";
?>
Put your html regsitration form here

<强>的login.php

<?php
include("func.php");
$username = $_POST["username];
$password = $_POST["password"];

if(!EmptyStr($username) && !EmptyStr($password)){
   sql = WRITE YOUR SQL SYNTAX HERE TO QUERY THE USERNAME AND PASSWORD TO DB
   if ($rs && !$rs->EOF) {  //user found and pass match
      $_SESSION["username"] = $username;
      redirect("/main.php");
   }else{
      $_SESSION["ErrMsg"] = "Invalid username or passsword!";
   }
}else{
   $_SESSION["ErrMsg"] = "Error! All fields are required.";
}

echo $_SESSION["ErrMsg"];
$_SESSION["ErrMsg"] = "";
?>

Put your html login form here

<强> main.php

<?php
include("func.php");
if(EmptyStr($_SESSION["username"])){ //check if user has session (logged in)
   $_SESSION["ErrMsg"] = "You need to logged in first to view this page!";
   redirect("/login.php");
}
?>

答案 3 :(得分:-1)

`Header("Location:login.php");`

或: echo "<meta http-equiv=refresh content='0; url=login.php'>";

或:

echo "<script language='javascript'>";
echo "location='login.php';";
echo "</script>";