[英]Redirect to the same page after user login PHP
我想讓用戶返回到用戶上次訪問的頁面。 例如,用戶單擊主頁上的登錄按鈕,它將轉到登錄表單頁面。 單擊登錄后,它將重定向回主頁。 我現在使用的編碼可以很好地用於登錄表單頁面到主頁(即Facebook登錄)的操作。 但是,當我嘗試做一個我之前說過的例子時,它不起作用。 出現“此頁面無法正常工作”錯誤。 我發現問題出在session.php上,我嘗試進行一些更改,但仍無法按我想要的方式工作。 有人可以在PHP編碼方面幫助我嗎? 我還是新使用這種PHP編碼。
下面是我的編碼..
index.php(HOMEPAGE)
<?php
include('session.php');
?>
<div class="header-right">
<?php if (isset($_SESSION['login_user'])) { ?>
<a class="active" >
<img src="image/user.png" width="22" height="22">
<?= $login_session ?>
<img src="image/drop.png" width="20" height="20" >
</a>
<div class="box dropdown">
<ul>
<a href="#">edit</a>
<a href="#">delete</a></li>
<a id="logout" href="logout.php">Log Out</a>
</ul>
</div>
<?php } else { ?>
<a class="active" href="login.php"><img src="image/lock.png" width="20" height="20"> Login</a>
<?php } ?>
</div>
</div>
session.php
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("company", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Same Page
}
?>
login.php(登錄表單頁面)
<?php
include('loginsuccess.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header('Location: '.$_SERVER['HTTP_REFERER']);
}
?>
<form class="form" action="" method="post">
<input type="text" placeholder="Username" required name="username">
<input id="myInput" type="password" placeholder="Password" required name="password">
<p id="text">Caps lock is ON</p>
<button type="submit" id="login-button" name="submit" value" Login">Login</button>
<span><?php echo $error; ?></span>
</form>
loginsuccess.php
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("company", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>
修改您的session.php,如下所示:
添加exit();
header('Location: '.$_SERVER['HTTP_REFERER']);
之后header('Location: '.$_SERVER['HTTP_REFERER']);
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("company", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Same Page
exit();
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.