1

他的以下代码有问题。出现的两条错误消息是

Notice: Use of undefined constant myusername - assumed 'myusername' in C:\Program Files\wamp\www\login_success.php on line 3

Deprecated: Function session_is_registered() is deprecated in C:\Program Files\wamp\www\login_success.php on line 3
Login Successful

但是仍然显示登录成功,即使最初没有输入任何信息。

您在任何地方的帮助将不胜感激。

HTML

会员登录 用户名: 密码:    

CSS

<?php

$host="localhost";
$username="root";
$password="";
$db_name="Hockey Club";
$tbl_name="members";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['username'];
$mypassword=$_POST['password'];


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);


$count=mysql_num_rows($result);



if($count==1){


session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

和 - login_success.php

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>
4

2 回答 2

3
if(!session_is_registered(myusername)){

你忘了使用引号。请对您的代码进行基本调试。

所以:

if (!session_is_registered('myusername')) {

而且,正如警告所示,session_is_registered在您的 PHP 版本中已弃用,实际上在较新的版本中已将其删除。请阅读文档。

反而:

if (!isset($_SESSION['myusername'])) {
于 2013-01-11T18:34:32.557 回答
0

你不应该使用session_is_registered().

改变

session_register("myusername");
session_register("mypassword");

$_SESSION['myusername'] = $username;
$_SESSION['mypassword'] = $password;

if(!session_is_registered(myusername)){

if(!isset($_SESSION['myusername'])) {
于 2013-01-11T18:38:47.703 回答