简体   繁体   English

在用户登录 php 后,我如何成功地将用户重定向到他所在的位置(他的当前页面)?

[英]how can i successfully redirect user to where he was(his current page) after he logged in in php?

I have been trying to redirect user to his current page(where he was before logged in) but still it is not working.我一直在尝试将用户重定向到他的当前页面(他登录之前所在的页面),但仍然无法正常工作。 I tried with my script below;我在下面尝试了我的脚本; but instead of taking the user to where he was..the user is taken to the home page.但不是将用户带到他所在的位置..用户被带到主页。 Can somebody subject the right way to do it?有人可以用正确的方法来做吗?

<?php
session_start();

if (!isset($_SESSION['cart'])) {
 $_SESSION['cart'] = array();
 } 

if (isset($_SESSION['user_id']) && filter_var($_SESSION['user_id'], FILTER_VALIDATE_INT,array('min_range' => 1)) ) { 
header('Location:accueil.html.php');  
}
if (array_key_exists('login', $_POST)) {

$email=$pass="";

 $errors = array();

// Check for an email address:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email = trim($_POST['email']);
 } else {
   $errorEmail ="Entrer un email valide";
    }

if (empty($_POST['pass'])) {
 $errorPass = "Entrer votre mot de passe";
} 
  else{
    $pass = trim($_POST['pass']);
  }

if ( $email && $pass) { //All IS GUD

include('includes/connect.inc.php'); 
 try {
$sql = "SELECT user_id FROM registered_user WHERE email = :email AND pass = :pass";
$stmt = $conn->prepare($sql);   
$stmt->bindValue(':email', $email);
$stmt->bindValue(':pass', SHA1($pass));
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

  if ($row) { //email and pwd combination is rigth
    $_SESSION['user_id'] = $row['user_id'];
    header('Location:'. $_SERVER['HTTP_REFERER']);
    exit();
  } 
    else{
    $errors[] = "Nom d'utilisateur et/ou mot de passe incorrect(s)";
    }

 } catch (PDOException $e) {
    $systemErr = "Désolé, Erreur de system".$e->getMessage();
 }

} else{
    $errors[] = "Désolé, votre connexion a échoué";
}

}//END MAIN IF

?>

You can take one session variable in each page.您可以在每一页中使用一个会话变量。

See below见下文

1) If you have header.php common file then you can add this line to store last url in header.php otherwise you can add in each page. 1) 如果你有 header.php 公共文件,那么你可以添加这一行来存储 header.php 中的最后一个 url,否则你可以在每个页面中添加。

$_SESSION['last_url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

2) Then you can redirect user to that url when he logged in. 2)然后您可以在用户登录时将用户重定向到该网址。

if ($row) { //email and pwd combination is rigth
    $_SESSION['user_id'] = $row['user_id'];
    header('Location:'. $_SESSION['last_url']);
    exit();
  } 

try this,尝试这个,

$usr_header = "Location:".$_SERVER['HTTP_REFERER'];
header($usr_header );
exit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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