对于重复的问题感到抱歉,但它对我不起作用;
access page only if logged in with php
如果有人登录,我必须访问display.php页面,如果他们直接输入url,则需要重定向到login.php页面, 我试过会议,但它不适合我,请帮我调试。
Display.php的
<?php
session_start();
if(!isset($_SESSION['loggedin']))
{
header("location: login.php");
}
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$disp = "select * from formdata order by user_id desc";
$result = mysqli_query($conn,$disp);
?>
<button onclick="location.href='formnew1.html';">Add</button>
<table border="2" cellpadding="0" cellspacing="0">
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
<th> Age </th>
<th> Gender </th>
<th> Address </th>
<th> City </th>
<th> Skills </th>
<th>Action</th>
</tr>
<?php
//$rows = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc())
{
$id = $row['user_id']; ?>
<tr>
<td><?php echo $row['user_id']?> </td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['age']?></td>
<td><?php echo $row['gender']?></td>
<td><?php echo $row['address']?></td>
<td><?php echo $row['city']?></td>
<td><?php echo $row['skill']?></td>
<td>
<a id="edit" href="edit1.php?id=<?php echo $row['user_id']; ?>">Edit</a>
<a href="#" id= "<?php echo $row['user_id'] ?>" onclick="deleteRow(this)">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
function deleteRow(obj){
conf=confirm('Are you sure to delete the Data');
if(conf){
var tr = $(obj).closest('tr');
$.post("delete1.php", {id: obj.id}, function(result){
tr.fadeOut('slow', function(){
$(obj).remove();
});
});
}
}
</script>
<?php
if (isset($_SESSION['success']))
{
echo '<script> alert("Data Added Successfully");</script>';
}
else if (isset($_SESSION['fail'])){
echo '<script> alert("Failed to Store");</script>';
//header("Location: /training/formnew.html");
}
mysqli_close($conn);
?>
的login.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<style>
div {
color: rgb(255,0,0);
}
form {
max-width: 425px;
margin: 10px auto;
padding: 10px 20px;
background: #ff994580;
border-radius: 10px;
}
fieldset {
margin-top: 100px ;
margin-bottom: 500px;
border: none;
}
h2 {
margin: 0 0 30px 0;
text-align: center;
font-family: 'Calibri';
font-size: 40px;
font-weight: 300;
}
label {
font-family: 'Calibri';
font-size: 16px;
font-weight: 50;
}
.submit {
background-color: #4CAF50;
border-radius: 10px;
color: white;
padding: 10px 40px 10px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.reset {
background-color: #ff3333;
border-radius: 10px;
color: white;
padding: 10px 40px 10px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<fieldset>
<form id="myform" name="myform" method="POST" action="validate.php">
<H2> LOGIN </H2>
<table width="60%" cellpadding="10">
<tr>
<td>
<label>User ID</label>
</td>
<td>
<input type="text" id="user_id" name="user_id"placeholder="Enter your User ID" required="required"/>
</td>
</tr>
<tr>
<td>
<label>User Name</label>
</td>
<td>
<input type="text" id="user_name" name="user_name" placeholder="Enter your Username" required="required"/>
</td>
</tr>
<tr>
<td>
<label>Password</label>
</td>
<td>
<input type="password" id="password" name="password" placeholder="Enter your Password" required="required"/>
</td>
</tr>
<tr>
<td>
<input type="submit" class="submit" name="submitbtn" value="Login">
</td>
<td>
<input type="reset" class="reset"/>
</td>
</tr>
</table>
</form>
</fieldset>
</script>
</body>
</html>
validate.php
<?php
session_start();
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$userid=$_POST['user_id'];
$username=$_POST['user_name'];
$password=$_POST['password'];
$qz = "SELECT * FROM userdata WHERE user_id = '$userid' AND user_name = '$username' AND password = '$password'";
$result=mysqli_query($conn,$qz);
if(mysqli_num_rows($result) == 1 )
{
$_SESSION['loggedin'] = true;
$_SESSION['user_id'] = $userid;
header('location: display1.php');
}
else{
$_SESSION['loggedin'] = false;
echo '<script> alert("ERROR: Please Check Credentials OR SignUp!!!"); window.location.href="login.php"; </script>';
}
}
mysqli_close($conn);
?>
答案 0 :(得分:0)
删除$_SESSION['loggedin'] = false;
文件中的行validate.php
。
或者将display.php
文件中的if语句更改为
if (!isset($_SESSION['loggedin'] || !$_SESSION['loggedin'])
您将loggedin
设置为false,因此当您调用isset
时,它会返回true,因为即使将其设置为false也会设置为。