I want to make the user go back to the the page that the user last visit. For example, user click at the login button at the homepage and it will go to login form page. After click login, it will redirect back to the homepage. The coding that I use right now work fine for login form page to homepage(ie Facebook login). However, when I try to make like example that I said before, it's not working. It's give "This page isn't working" error. I find that the problem is at the session.php and I try to change a bit but still not working like I want. Can someone help me at the PHP coding. I still new using this PHP coding.
Below is my coding..
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(Login Form Page)
<?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
}
}
?>
Modify your session.php as follows:
Add exit();
after 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();
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.