How to redirect user to his current page in php after logged in?

Hi;

I am trying to redirect user back to where he was after logged in?i haved tried the code below but it is not working…can somebody subject the rigth way?

[php]<?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();
$numRows = $stmt->rowCount();

if ($numRows === 1) { //email and pwd combination is rigth
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION[‘user_id’] = $row[‘user_id’];
header('Location: '.$_SERVER[‘REQUEST_URI’]);
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

?>[/php]

A few comments first.

You hash for the password is far too weak. password_hash is what you are after.

[php]if ($numRows === 1) { //email and pwd combination is rigth[/php]
will always be false. rowCount returns affected rows on Insert, Update, and Delete, not select.

[php] $query = “SELECT user_id, pass FROM registered_user WHERE email = :email”;
$stmt = $pdo->prepare($query);
$stmt->bindParam(’:email’, $email);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) { // Check to see if user is in the database table:
if (password_verify($pass, $row[‘pass’])) { // If so, then check to see if passwords match:
$_SESSION[‘user_id’] = $row[‘user_id’]; // Set sessions to the corresponding user’s id:
}
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service