2013-10-20 98 views
0

嘿有stackoverflow社區!PHP 5.4.12:未定義的索引錯誤

我目前正在研究跨遊戲+服務器間項目,但我陷入了我的身份驗證系統。當我和一些正確的詳情登錄我的錯誤

Notice: Undefined index: myusername in C:\wamp\www\new\panel.php on line 8 
Notice: Undefined index: mypassword in C:\wamp\www\new\panel.php on line 9 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\new\panel.php on line 13 

我不知道如何解決這個問題,所以我要問什麼是如何解決這個問題?

這是我使用檢查登錄信息的代碼:

<?php 

ob_start(); 
$host="localhost"; // Host name 
$username="mindfulbank"; // Mysql username 
$password=""; // Mysql password 
$db_name="mindful_bank"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("Database cannot connect securely, please check back later!"); 
mysql_select_db("$db_name")or die("Database Cannot Be Selected"); 

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$password=$_POST['mypassword']; 
$sha1password=sha1($password); 
$mypassword=md5($sha1password); 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

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

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 
$_SESSION['myusername']=$myusername; 
$_SESSION['mypassword']=$mypassword; 
session_start(); 
header("location:panel.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 

ob_end_flush(); 
?> 

和頁面,這些錯誤acctually導致錯誤:

<?php 
session_start(); 

if(isset($_SESSION['myusername'])){ 
header("location:logout.php"); 
} 

$myusername=$_SESSION['myusername']; 
$mypassword=$_SESSION['mypassword']; 
$con=mysqli_connect("localhost","mindfulbank","","mindful_bank"); 
$result = mysqli_query($con,"SELECT level FROM members WHERE password = '$mypassword' and username = '$myusername'"); 
$fname = mysqli_query($con,"SELECT name FROM members WHERE password = '$mypassword' and username = '$myusername'"); 
while($row = mysqli_fetch_array($result)) 
{ 
    if($row['admin'] == 1){ 
    require 'panel/admindash.php'; 
} 
else 
{ 
    require 'panel/userdash.php'; 
} 
} 
?> 

回答

1

將用於檢查登錄詳細信息的腳本的連接更新爲mysqli,因爲mysql已折舊。

完成此操作後,將mysql_real_escape_string($myusername);函數更改爲mysqli_real_escape_string($your_connection, $myusername);

mysqli_real_escape_string需要db連接作爲所需參數。

http://php.net/manual/en/mysqli.real-escape-string.php

然後加入session_start();用來檢查登錄信息的腳本的頂部!

http://php.net/manual/en/function.session-start.php

希望這有助於

0

你需要一個在session_start()在第一個文件也是如此。

0

前兩個錯誤發生,因爲你沒有在第一個腳本$_SESSION之前寫session_start()。總是將它寫在腳本的頂部。最後一個錯誤是因爲您沒有通過運行該查詢來獲得任何結果。進一步操作前請務必檢查$result->num_rows > 0