如何重定向到登录页面,然后重定向回php中的上一页

问题描述 投票:0回答:1

我知道对此有很多问题,但是当我希望登录名始终重定向到主页时,我找不到答案,除非用户单击“提交”页面时它应该登录然后允许用户添加一个建议。单击“提交”页面后,我设法重定向到登录页面,但是此后,它重定向到主页,并且陷入了循环。(索引是我的主页)。(建议是我要强制登录的位置)。到目前为止,这是我所做的:在我的proposal.php的顶部:

<?php
if(!isset($_SESSION['user_id'])) 
{
    $_SESSION["login_redirect"] = $_SERVER["PHP_SELF"];
    header("Location: login2.php");
    exit;
}
?>

Login-form.php:

<?php
if(isset($_POST['loginbutton'])){
require 'dbh.inc.php'; 

$UsernameEmail = $_POST['username/email'];
$password = $_POST['password'];


if (empty($UsernameEmail)||empty($password)){
    header("location: ../Index.php?error=emptyfields&username");
    exit();
}else {
    $sql = "SELECT * FROM users WHERE username=? OR email =? ;" ;
    $stmt = mysqli_stmt_init($conn);

    if(!mysqli_stmt_prepare($stmt,$sql)){
header("location: ../Index.php?error=sqlerror");
    exit();

    }
    else {

    mysqli_stmt_bind_param($stmt , "ss", $UsernameEmail,$UsernameEmail );
    mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt); 
   if($row = mysqli_fetch_assoc($result)){
       $pwdCheck = password_verify($password , $row['password']);
       if($pwdCheck == false ){
           header("location: ../Index.php?error=wrongPassword");
         exit();  
       }  else if ($pwdCheck == true ){
           session_start();
           $_SESSION['user_id']= $row['id'];
            $_SESSION['user_name']=  $row['username'];

       } else {
              header("location: ../Index.php?error=wrongPassword");
         exit();  
       }

   }
   else { 
       header("location: ../Index.php?nouser/emailmatch");
    exit(); 
   }

    }

}   
}
else {

header("Location: ../Index.php?succses");
    exit();
}

我也在SUGGEST.php中尝试了此代码

<?php
if(!isset($_SESSION['user_id'])) 
{
    header('Location: login2.php?redirect=SUGGEST.php');
    exit;
} 
?>

和这个在login-form.php中的文件,但是那也不起作用

if (isset($_GET['redirect'])) {
    header('Location: ' . $_GET['redirect']);
} 

这是我第一次使用php进行编码,因此,我真的会提供详细的答案。谢谢

php mysql authentication redirect require
1个回答
0
投票

您应该在每页的第一行处使用session_start();,实际上在此之前没有空格或换行符!

© www.soinside.com 2019 - 2024. All rights reserved.