简体   繁体   中英

Redirecting in a PHP page to another PHP page

I have a register.php, login.php, and main.php. How do i redirect user after after registration submit to login page and then login page submit to main page.

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

See the exit . Don't ever forget to do this. If you have sensitive data after this, it will be visible to anyone that doesn't follow location headers ( such as some bots ).

To stop you from forgetting, you could build a wrapper type function

function redirect($url) {
    header('Location: ' . $url);
    exit;
}
header("Location: /login.php");

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

You can do this:

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");
}
?>
`Header("Location:login.php");`

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

or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM