未定义索引:C:\ wamp \ www \ Website \ storeadmin \ admin_login.php ..中的用户名,密码相同 [英] Undefined index: username in C:\wamp\www\Website\storeadmin\admin_login.php..and the same for password
问题描述
//index.php
<?php
session_start();
if(!isset($_SESSION["member"])){
header("location:admin_login.php");
exit();
}
$managerID = preg_replace('#[^0-9]#i',"",$_SESSION["id"]);
$manager = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["name"]);
$password = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["password"]);
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM admin WHERE id ='managerID' AND username ='$manager' AND password ='$password'LIMIT 1");
$exist_count = mysql_num_rows($sql);
if($exist_count == 0){
echo("Your login session data in not in the database");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Service Admin Area</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen"/>
</head>
<body>
<div id="mainWrapper" >
<?php include_once("../template_header.php");?>
<div id="pageContent" >
<div align="left" "style="margin-left:040px;"><h1>Hello Store Manager .What would you loke to do today</h1><br />
<h3><a href="inventory_list.php">Manage Inventory</a></h3><br/><h3><a href="">Manage Me</a></h3><br/></div></div>
<?php include_once("../template_header.php");?>
</div>
</body>
</html>
//admin_login.php
<?php
session_start();
if(isset($_SESSION["member"])){
header("location:index.php");
exit();
}
$manager = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["username"]);
$password = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["password"]);
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM admin WHERE username ='$manager' AND password ='$password'LIMIT 1");
$exist_count = mysql_num_rows($sql);
if($exist_count == 1){
while(mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["id"]= $id;
$_SESSION["name"]= $manager;
$_SESSION["password"]= $password;
header("location:index.php");
exit();
}
else{
echo 'This information is incorrect,try again <a href = "index.php">Click Here</a>';
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> AdminLogin</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen"/>
</head>
<body>
<div id="mainWrapper" >
<?php include_once("../template_header.php");?>
<div id="pageContent" >
<div align="left" "style="margin-left:040px;"><h1>Please login to continue</h1><br />
</div>
<form id="form1" name="form1" method="post" action=" admin_login.php">
UserName<br />
<input type="text" name="username" id="username" size="40"/>
Password<br />
<input type="password" name="password" id="password" size="40"/>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="LogIn"/>
</form>
</div>
<?php include_once("../template_header.php");?>
</div>
</body>
</html>
这变得很烦人,我无法得到错误,我知道它很小,真的希望我可以纠正你们之间的错误.以上两个php页面用于使我的练习Web文档中的Admin Login(管理员登录)页面
This is getting tiresome,and i cant get the error,and i know it is pretty small and really hope i can the error rectified among you guys.The above two php pages are tomake the Admin Login pages in my practise web documents.
推荐答案
您在一个脚本中使用$_SESSION['name']
,在另一个脚本中使用$_SESSION['username']
.
You are using $_SESSION['name']
in one script and $_SESSION['username']
in another.
我想如果将username
更改为name
,反之亦然,该错误将消失.
I would imagine if you change username
to name
or vice versa the error will go away.
我不确定密码错误是什么,我的直觉是您正在看到它,因为会话才刚刚开始并且$ _SESSION数组为空,请尝试删除一些if(isset( $ _SESSION ['...'])).
在admin_login中,您应检查是否已单击登录"按钮,并且不只是在加载表单,即
In the admin_login you should check that the login button has been clicked and that you are not just loading the form i.e.
//admin_login.php
<?php
session_start();
if(isset($_SESSION["member"])){
header("location:index.php");
exit();
}
if(@$_POST['button'] == 'LogIn'){ // <- Check the user has clicked the button
$manager = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["username"]);
$password = preg_replace('#[A-Za-z0-9]#i',"",$_SESSION["password"]);
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM admin WHERE username ='$manager' AND password ='$password'LIMIT 1");
$exist_count = mysql_num_rows($sql);
if($exist_count == 1){
while(mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["id"]= $id;
$_SESSION["name"]= $manager;
$_SESSION["password"]= $password;
header("location:index.php");
exit();
}else{
echo 'This information is incorrect,try again <a href = "index.php">Click Here</a>';
exit();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
....
其他一些注意事项:
- 您在登录表单中有两个
<html>
标签 - 登录表单的action属性在脚本名称之前有一个空格
- 如果在SQL中使用LIMIT 1,则无需将
mysql_fetch_array
包装在while()
中
- You have two
<html>
tags in the login form - The action attribute of the login form has a space before the script name
- If you use LIMIT 1 in your SQL you don't need to wrap the
mysql_fetch_array
in awhile()
这篇关于未定义索引:C:\ wamp \ www \ Website \ storeadmin \ admin_login.php ..中的用户名,密码相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!