仅当用户登录php时才需要显示页面
对不起,重复了一个问题,但它不适合我 如果有人登录,我必须访问display.php页面,如果他们直接输入url,则需要重定向到login.php页面, 我尝试了会话,但它对我不起作用,请帮助我调试它 display.php仅当用户登录php时才需要显示页面,php,sql,session,Php,Sql,Session,对不起,重复了一个问题,但它不适合我 如果有人登录,我必须访问display.php页面,如果他们直接输入url,则需要重定向到login.php页面, 我尝试了会话,但它对我不起作用,请帮助我调试它 display.php <?php session_start(); if(!isset($_SESSION['loggedin'])) { header("location: login.
<?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);
?>
<?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);
?>
添加
身份证件
名称
电子邮件
年龄
性别
地址
城市
技能
行动
函数deleteRow(obj){
conf=confirm('您确定要删除数据');
if(conf){
var tr=$(obj).closest('tr');
$.post(“delete1.php”,{id:obj.id},函数(结果){
tr.fadeOut('slow',function(){
$(obj.remove();
});
});
}
}
删除行$\u SESSION['loggedin']=false
在validate.php
文件中
或者将display.php
文件中的if语句更改为
if(!isset($_SESSION['loggedin']| |!$_SESSION['loggedin'])
您正在将loggedin
设置为false,因此当您调用isset
时,它会返回true,因为即使设置为false,它也会被设置为true。我可以使用这些文件进行登录验证。但是如果我直接输入url,它会在validate.php中显示内容。不要使用$\u SESSION['loggedin']=false
只需删除它,因为即使它为false也可以设置它,但是如果(!isset($\u SESSION['loggedin']))|$\u SESSION['loggedin']==false),也可以执行(因为如果验证失败,则在validate.php中将其设置为false)。或者,您可以将其设置为null
而不是false
。不要将密码存储为纯文本!这一点都不安全!PHP有内置函数,您应该使用这些函数来处理密码存储,请查看安全性更高的函数!当然它不可能是活动的,OP使用root
作为用户帐户。Su相信没有人会在实时系统中这样做:)
<?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);
?>