I'm coding a new site allowing users to post candidatures ! First, They need to connect in order to access at candidature page. My problem is that if connection input are incorrect, I redirect the user to the connection page with an error message, however my message is not displaying :/ I based myself on this subject but unsuccessfully :/ Furthermore, I don't want to pass by the url to show the message
displaying a message after redirecting the user to another web page
checkForm.php
<?php
require('../config/config.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['identity']) && isset($_POST['password']))
{
$username = $_POST['identity'];
$password = $_POST['password'];
$stmt = $cnx->prepare("SELECT * from users WHERE nameUser = '".$username."' AND passwordUser = '".sha1($password)."'");
$stmt->execute();
$users = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($users);
$result = $stmt->rowCount();
if($result > 0)
{
session_start();
$_SESSION['user'] = $_POST['identity'];
$_SESSION['email'] = $users['emailUser'];
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (120 * 60);
$_SESSION['pwd'] = $_POST['password'];
$_SESSION['id'] = $users['userId'];
header('Location: ../index.php');
}
else
{
header('Location: login.php');
//echo "<div class='alert alert-warning'>Connection failed ! Please check your information !</div>";
session_start();
$_SESSION['message'] = "<div class='alert alert-warning'>Connection failed ! Please check your information !</div>";
}
}
}
else
{
header('Location: login.php');
session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the indicated fields !</div>";
}
?>
login.php
<?php
include('../index.php');
include('../template.php');
?>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<form method="post" action="../templates/checkForm.php">
<div class="container">
<?php
session_start();
if(isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
<div class="card card-container">
<img id="profile-img" class="profile-img-card" src="../img/avatar.png" />
<p id="profile-name" class="profile-name-card"></p>
<form class="form-signin">
<span id="formIdentity" class="formIdentity"></span>
<input type="text" id="identity" name="identity" class="form-control" placeholder="Your identity" autofocus>
<input type="password" id="password" name="password" class="form-control" placeholder="Your password">
<button class="btn btn-lg btn-primary btn-block btn-signin" type="submit" id="submitBtn" name="submit" onclick="checkForm(this)">Connect</button>
</form><!-- /form -->
<a href="../templates/forgotPassword.php" class="forgot-password">
forgot Password ?
</a>
</div><!-- /card-container -->
</div><!-- /container -->
</form>
</body>
</html>
Change order, first set session value, then call header. Eg.:
session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the
indicated fields !</div>";
header('Location: login.php');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments