2012-06-27 38 views
0

可能重複:
Undefined index: username in C:\wamp\www\Website\storeadmin\admin_login.php..and the same for password無法登錄到所需的位置

//admin_login.php 
<?php 
session_start(); 
if(isset($_SESSION["member"])){ 
    header("location:index.php"); 
    exit(); 
} 

?> 
<?php 
if(isset($_POST["username"]) && isset($_POST["password"])){  // <- Check the user has clicked the button 
    $manager = preg_replace('#[A-Za-z0-9]#i',"",$_POST["username"]); 
    $password = preg_replace('#[A-Za-z0-9]#i',"",$_POST["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["manager"]= $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"> 
<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> 
//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["manager"]); 
$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> 

我現在面臨的問題是,我不能夠登錄到我index.php頁甚至在我已經通過phpmyadmin設置了我的數據庫中指定的正確用戶名和密碼後。每次我嘗試登錄時,它都會調用echo 'This information is incorrect,try again Click Here',如admin_login.php中所述。我感到有點沮喪。你能幫我出去嗎?

+0

請嘗試發佈只有相關的代碼。你試過什麼了? – TJHeuvel

+0

是你的sql甚至運行?是$密碼和限制之間不需要的空間嗎? SELECT * FROM admin WHERE username ='$ manager'AND password ='$ password'LIMIT 1 –

+0

@NicholasKing sql正在運行。在你提到的空間事件之後,問題仍然存在。 – user1485323

回答

1

嘗試一些調試;

  • 在preg_replace調用之前和之後檢查$ _POST ['username']和$ _POST ['password']的值。
  • 添加「或死亡mysql_error()」您來電的mysql_query(),看看是否運行正常
  • 回聲出$ sql中的值,這樣你可以看看查詢。
  • header()命令不需要名稱和值之間的空格嗎?或之後的\ r \ n?值得仔細檢查。
  • 通常在各個階段提供更多的產出,追蹤問題應該是微不足道的。

(一旦它正在工作),請在數據庫的密碼字段中添加crypt()或md5()+ salt。

0

即時通訊相當肯定的preg_replace根據PHP文檔http://uk.php.net/manual/en/function.preg-replace.php

你試圖刪除它們清空你的變量

$manager = preg_replace('#[A-Za-z0-9]#i',"",$_POST["username"]); $password = preg_replace('#[A-Za-z0-9]#i',"",$_POST["password"]);