Redirect after a message "you have succesfully logged in" and "welcome back."
Hey guys, in this piece of code. Is there a way to redirect the user to the homepage after the messages "Welcome bace you will now be redirected to the homepage." and "You have succesfully logged in. you will now be redirected to the homepage." ?
OK I updated my code. Here it is:
<?php
function redirect() {
header('location: index.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" >
<head>
<title>Login | JM Today </title>
<link href="Mainstyles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<?php include("header.php"); ?>
<?php include("navbar.php"); ?>
<?php include("checkcook.php") ?>
<div id="wrap">
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$conn=mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db('jmtdy', $conn) or die(mysql_error());
if(isset($_COOKIE['jmuser']) && isset($_COOKIE['jmpass'])){
$status=checkCookie($_COOKIE['jmuser'], $_COOKIE['jmpass']);
if($status==true){
echo '<p class="statusmsg"> Welcome back'.$_COOKIE['jmuser'].'. You will now be redirected to the homepage.</p>';
sleep(5);
redirect();
}
}
else{
if(isset($_POST['sublogin'])){
if(( strlen($_POST['user']) >0) && (strlen($_POST['pass']) >0)) {
checklogin($_POST['user'], $_POST['pass']);
}
elseif((isset($_POST['user']) && empty($_POST['user'])) || (isset($_POST['pass']) && empty($_POST['pass']))){
echo '<p class="statusmsg">You didn\'t fill in the required fields.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
}
}
else{
echo '<p class="statusmsg">You came here by mistake, didn\'t you?</p>';
}
function checklogin($username, $password){
$username=mysql_real_escape_string($username);
$password=mysql_real_escape_string($password);
$result=mysql_query("select * from users where username = '$username'");
if($result != false){
$dbArray=mysql_fetch_array($result);
$dbArray['password']=mysql_real_escape_string($dbArray['password']);
$dbArray['username']=mysql_real_escape_string($dbArray['username']);
if(($dbArray['password'] != $password ) || ($dbArray['username'] != $username)){
echo '<p class="statusmsg">The username or password you entered is incorrect. Please try again.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
开发者_运维百科 return;
}
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if(isset($_POST['remember'])){
setcookie("jmuser",$_SESSION['username'],time()+60*60*24*356);
setcookie("jmpass",$_SESSION['username'],time()+60*60*24*356);
}
echo'<p class="statusmsg"> You have successfully logged in. You will now be redirected to the homepage.</p>';
sleep(5);
redirect();
}
else{
echo'<p class="statusmsg"> The username or password you entered is incorrect. Please try again.</p><br/>input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
}
}
?>
</div>
<br/>
<br/>
<?php include("footer.php") ?>
</div>
</body>
</html>
But now, whatever I do (blank login, wrong password/username, ...) I don't get any message, and I'm not even redirected. Its just the header and a blank page.
Redirect with timer.
Below the messages add
<meta http-equiv="refresh" content="10;url=http://where.com">
Following assumes that your homepage is a file called index.php and that your include files don't have any header info in them otherwise you would have to put the redirect function at the beginning of your first include file header.php:
<?php
function redirect() {
header('location: index.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" >
<head>
<title>Login | JM Today </title>
<link href="Mainstyles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<?php include("header.php"); ?>
<?php include("navbar.php"); ?>
<?php include("checkcook.php") ?>
<div id="wrap">
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$conn=mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db('jmtdy', $conn) or die(mysql_error());
if(isset($_COOKIE['jmuser']) && isset($_COOKIE['jmpass'])){
$status=checkCookie($_COOKIE['jmuser'], $_COOKIE['jmpass']);
if($status==true){
echo '<p class="statusmsg"> Welcome back'.$_COOKIE['jmuser'].'. You will now be redirected to the homepage.</p>';
redirect();
}
else{
if(isset($_POST['sublogin'])){
if(( strlen($_POST['user']) >0) && (strlen($_POST['pass']) >0)) {
checklogin($_POST['user'], $_POST['pass']);
}
elseif((isset($_POST['user']) && empty($_POST['user'])) || (isset($_POST['pass']) && empty($_POST['pass']))){
echo '<p class="statusmsg">You didn\'t fill in the required fields.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
}
}
else{
echo '<p class="statusmsg">You came here by mistake, didn\'t you?</p>';
}
function checklogin($username, $password){
$username=mysql_real_escape_string($username);
$password=mysql_real_escape_string($password);
$result=mysql_query("select * from users where username = '$username'");
if($result != false){
$dbArray=mysql_fetch_array($result);
$dbArray['password']=mysql_real_escape_string($dbArray['password']);
$dbArray['username']=mysql_real_escape_string($dbArray['username']);
if(($dbArray['password'] != $password ) || ($dbArray['username'] != $username)){
echo '<p class="statusmsg">The username or password you entered is incorrect. Please try again.</p><br/><input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if(isset($_POST['remember'])){
setcookie("jmuser",$_SESSION['username'],time()+60*60*24*356);
setcookie("jmpass",$_SESSION['username'],time()+60*60*24*356);
}
echo'<p class="statusmsg"> You have successfully logged in. You will now be redirected to the homepage.</p>';
redirect();
}
else{
echo'<p class="statusmsg"> The username or password you entered is incorrect. Please try again.</p><br/>input type="button" value="Retry" onClick="location.href='."'login.php'\">";
return;
}
}
}
?>
</div>
<br/>
<br/>
<?php include("footer.php") ?>
</div>
</body>
</html>
This is not possible using plain php because you can not output content before header redirect because then headers have already been sent.
You have got a couple of options:
- meta refresh
- javascript redirect
- create an API and use JavaScript to inject HTML into the DOM(preferably using JQuery Ajax load to ease the development.
The easiest way to achieve this is meta refresh. The cleanest way(if you ask me) is using Jquery.load()
My proposition is more 'advanced', but if you want to do better things one day I think you should get a try.
A nicer approach would be using ob_start/ob_flush functions to avoid problems with headers already send (as your response will be send only when youy flush the response buffer, all your echos are going the output, but at any time you can empty this output and build a new one, and it's faster for the web server as well, you should really try).
Then you could simply avoid showing this page with a message and send a nice redirect (302) or nicer 'redirect after post' redirection (303) to the home page. So that the client browser will automatically fetch the home page (and it works even with no js or an very old browser)
To show a message like 'you're now logged in' after this succesfull login you'll have the problem that the redirection will perform a completly new query from the browser to your server. So you'll have to store the message in the session in your login tretament, and check in every page that you do have some messages to show, then show it and flsuh the session message.
精彩评论