Please can some one help me. We have upgraded php and not i cant get the php page to correctly redirect the user to the login page if the user has not logged in and the get it to redirect to the index page. So the form sequence is as follows. User connects to index page, user clicks on link to add or delete data in mysql database, page must redirect to login page if user has not logged in, once logged in login page must redirect to add or delete page depending on the link clicked on. Addins and deleting data to the mysql database table works fine.
Below is the add,php page.
<?php
ini_set(âdisplay_errorsâ, 1);
ini_set(âdisplay_startup_errorsâ, 1);
error_reporting(E_ALL);
session_start(); //start session.
if(!isset($_GET[ânameâ])){//added this to check if name is sent
include(âdatabase.phpâ);
if (!isset($_SESSION[âuser.idâ]) && $_SESSION[âuser.idâ] !="")
{
}
else{
header(âLocation: login.phpâ);
}
if($_POST[âactionâ])
{
include(âemail.phpâ);
$address="";
$name=$_POST[ânameâ];
$extension=$_POST[âextensionâ];
$department=$_POST[âdepartmentâ];
$phone=$_POST[âphoneâ];
$email=$_POST[âemailâ];
$sql = âINSERT INTO users (ID, Name, Email, Extension, Phone, Department) VALUES (NULL, â$nameâ, â$emailâ, â$extensionâ, â$phoneâ, â$departmentâ)â;
if ($conn->query($sql) === TRUE)
echo âNew record addedâ;
else
echo "Error: " . $sql . â
â . $conn->error;
$conn->close();
}
}
Below is the login.php
<?php
ini_set(âdisplay_errorsâ, 1);
ini_set(âdisplay_startup_errorsâ, 1);
error_reporting(E_ALL);
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'];
// mysqli_connect() function opens a new connection to the MySQL server.
$conn = mysqli_connect("localhost", "root", "Pr1v@cY", "T-List_VW");
// SQL query to fetch information of registerd users and finds user match.
$query = "SELECT * from UserName where userName=? AND pass=? LIMIT 1";
// To protect MySQL injection for Security purpose
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();
if($stmt->fetch()) //fetching the contents of the row {
$_SESSION['login_user'] = $username; // Initializing Session
header("location: index.php"); // Redirecting To Profile Page
}
mysqli_close($conn); // Closing Connection
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password"><br><br>
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</body>
</html>