I have a form that helps the user to login in after login the user is directed to their dashboard where they can update their profile
<form class="form-horizontal" role="form" action="admin_insert_profile.php" enctype="multipart/form-data" method="post">
<h4>Profile Details </h4>
<div class="form-group">
<label class="col-lg-4 control-label">Name</label>
<div class="col-lg-6">
<input class="form-control" value="<?php echo $user_admin_name ;?>" type="text" name="name" >
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Email</label>
<div class="col-lg-6">
<input class="form-control" value="<?php echo $login_session ;?>" type="email" name="email" >
<p class="help-block"></p>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Password</label>
<div class="col-lg-6">
<input class="form-control" value="<?php echo $user_admin_password ;?>" type="password" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit" name="submit">
<span></span>
</div>
</div>
</form>
admin_insert_profile.php
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$sql="UPDATE member SET admin_name = '".$name."', admin_email = '".$email."',admin_password = '".$password."' WHERE id='1' ";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: admin_login.php");
exit;
mysqli_close($con);
?>
After successful updating their profile they are logged out and redirected to login page, on this login page, i wish to display a message that says that password has been updated and the user needs to login again. is there a way i can do so
Yes!.. You can put below code at:
admin_insert_profile.php
page
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$_SESSION['message']="password has been updated";
header("Location: admin_login.php");
exit;
And in your login.php
page put code befor <form>
tag:
<?PHP
if(isset($_SESSION['message'])){
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
And if you not use session_start()
then use in both page.
Yes you can do this with the help of global variable like $_server
and $_session
. If successful update set $_SERVER['flag']='set';
and on login page check following for $_SERVER['flag']
variable.
if($_SERVER['flag']=='set')
{
//passwod changed
}
One way of doing it will be like this, update your admin_insert_profile.php
as below
....
header("Location: admin_login.php?ok=1");
mysqli_close($con); // Move it to here, its never get called
exit;
in admin_login.php
//Check for success flag in get query param
$ok = isset($_GET['ok']);
//Insert this code where you wanted to show the msg
if($ok) {
echo '<div class="alert alert-success" role="alert">
Password has been updated and the user needs to login again
</div>';
}
for your information here is these nifty function which i use to do flash messaging
using session
function addMsg($msg, $type = 'danger') {
$_SESSION['msg'] = $type .'|'. $msg;
}
function showMsg($class = 'msg') {
$msg = @$_SESSION['msg'];
if(isset($msg) && !empty($msg)) {
$msgPart = explode('|', $msg);
$msgText = ($msgPart[1]);
$msgType = $msgPart[0];
if( !empty($msgText) ) {
//Remove Flash message
unset( $_SESSION['msg'] );
return sprintf('
<div id="flashMsg" class="alert alert-%s">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
%s
</div>
', $msgType, $msgText);
}
}
}
to use if just do following
//Start the session at top
start_session();
addMsg('Password has been updated and the user needs to login again', 'success');
header("Location: admin_login.php");
mysqli_close($con); // Move it to here, its never get called
exit;
in admin_login.php
//Start the session at top
start_session();
//put where you wanted to show the flash
echo showMsg();