这是我的代码
<html>
<head>
<title>login page</title>
</head>
<body bgcolor="black" style="color:gray">
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
session_start();
if(isset($_SESSION["logging"])&& $_SESSION["logged"])
{
print_secure_content();
}
else {
if(!$_SESSION["logging"])
{
$_SESSION["logging"]=true;
loginform();
}
else if($_SESSION["logging"])
{
$number_of_rows=checkpass();
if($number_of_rows==1)
{
$_SESSION[user]=$_GET[userlogin];
$_SESSION[logged]=true;
print"<h1>you have loged in successfully</h1>";
print_secure_content();
}
else{
print "wrong pawssword or username, please try again";
loginform();
}
}
}
function loginform()
{
print "please enter your login information to proceed with our site";
print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
print "<input type='submit' >";
print "<h3><a href='registerform.php'>register now!</a></h3>";
}
function checkpass()
{
$servername="localhost";
$username="root";
$conn= mysqli_connect($servername,$username)or die(mysqli_error());
mysqli_select_db($conn,"test");
$sql="select * from users where name='$_POST[userlogin]' and password='$_POST[password]'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
return mysqli_num_rows($result);
}
function print_secure_content()
{
print("<b><h1>hi mr.$_SESSION[user]</h1>");
print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a><br>";
}
?>
</form>
</body>
</html>
我收到这 3 个错误
注意:未定义索引:已登录 C: 第 10 行 注意:未定义索引:第 51 行 C 中的 userlogin 注意:第 51 行未定义索引:密码
我尝试过 isset() 但第 10 行没有任何变化。 结束错误2和3我不知道。 非常感谢。
我查看了引用主题,但它没有解决我的问题。
最佳答案
- 输入
session_start()
功能是<html>
之前的第一件事标签。 - 在第 10 行,您忘记将 isset() 放在第二个条件上,如
if(isset($_SESSION["logging"])&& isset($_SESSION["logged"]))
- 在第 28 和 29 行添加引号:
$_SESSION['user']=$_GET['userlogin'];
和$_SESSION['logged']=true;
- 像这样连接变量
print("<b><h1>hi mr" . $_SESSION[user] . "</h1>");
你的最终代码将是这样的:
<?php
session_start();
?>
<html>
<head>
<title>login page</title>
</head>
<body bgcolor="black" style="color:gray">
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>
<?php
if(isset($_SESSION["logging"])&& isset($_SESSION["logged"]))
{
print_secure_content();
}
else
{
if(!$_SESSION["logging"])
{
$_SESSION["logging"] = true;
loginform();
}
else if($_SESSION["logging"])
{
$number_of_rows = checkpass();
if($number_of_rows == 1)
{
$_SESSION['user'] = $_GET['userlogin'];
$_SESSION['logged'] = true;
print"<h1>you have loged in successfully</h1>";
print_secure_content();
}
else{
print "wrong pawssword or username, please try again";
loginform();
}
}
}
function loginform()
{
print "please enter your login information to proceed with our site";
print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
print "<input type='submit' >";
print "<h3><a href='registerform.php'>register now!</a></h3>";
}
function checkpass()
{
$servername="localhost";
$username="root";
$conn= mysqli_connect($servername,$username)or die(mysqli_error());
mysqli_select_db($conn,"test");
$sql="select * from users where name='$_POST[userlogin]' and password='$_POST[password]'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
return mysqli_num_rows($result);
}
function print_secure_content()
{
print("<b><h1>hi mr" . $_SESSION[user] . "</h1>");
print "<br><h2>only a logged in user can see this</h2><br><a href='logout.php'>Logout</a><br>";
}
?>
</form>
</body>
</html>
关于php - 未定义索引 :logged in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29051498/