[英]need to show page only if a user logged in php
Sorry for a duplicate question, but its not working for me; 对不起,我有一个重复的问题,但对我不起作用;
access page only if logged in with php 仅在使用php登录时访问页面
i have to access display.php page if someone loggedin, if they enter the url directly it need to redirect to login.php page, i tried sessions but its not working for me, please help me to debug it. 如果有人登录,我必须访问display.php页面,如果他们直接输入url,则需要重定向到login.php页面,我尝试了会话,但它对我不起作用,请帮助我进行调试。
display.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 的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 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);
?>
Either remove the line $_SESSION['loggedin'] = false;
要么删除行
$_SESSION['loggedin'] = false;
in your validate.php
file. 在您的
validate.php
文件中。
Or change the if statement in your display.php
file to be 或将
display.php
文件中的if语句更改为
if (!isset($_SESSION['loggedin'] || !$_SESSION['loggedin'])
You are setting the loggedin
to be false, so when you call isset
it returns true, because it is set even though it is set to false. 您将
loggedin
设置为false,因此在调用isset
它会返回true,因为即使已将其设置为false,它也会被设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.