2014-01-10 190 views
0

我爲我的網頁創建了一個登錄系統。 在本地主機上,當我輸入我的用戶名和密碼,並點擊登錄它的作品。 當我在現場直播時,什麼都沒有發生。登錄系統不想登錄網頁

這裏是我的代碼:

<?php 

if(isset($_POST['username']) && isset($_POST['password'])){ 

$password=$_POST['password']; 
$username=$_POST['username']; 
$password_hash=md5($password); 

if(!empty($username) && !empty($password)){ 
$query= "SELECT id FROM studenti WHERE username='".mysql_real_escape_string($username)."' AND password='".mysql_real_escape_string($password_hash)."'"; 
if($query_run=mysql_query($query)){ 
$query_num = mysql_num_rows($query_run); 

if($query_num==0){ 

echo '<div class="text1">Pogresan Username/Password</div>'; 

}else if ($query_num==1){ 

$user_id=mysql_result($query_run,0, 'id'); 
$_SESSION['user_id']=$user_id; 
header('Location : index.php'); 

} 
} 
}else{ 
echo '<div class="text2">Morate upisati Username i Password</div>'; 

} 
} 

?> 

在這裏,您可以嘗試EXAMPLE Registraion工作正常,並且所有用戶都在DATEBASE,也DATEBASE我的密碼字段是35字符長。(MD5)。我看不出哪裏是問題..

這是註冊代碼:

<?php 
require 'core.php'; 
require 'connectdb.php'; 

if(!loggedin()){ 

if(
    isset($_POST['password']) && isset($_POST['password_again'])&& isset($_POST['username'])&& isset($_POST['ime'])&& isset($_POST['prezime'])){ 

$password=$_POST['password']; 
$password_again=$_POST['password_again']; 
$username=$_POST['username']; 
$ime=$_POST['ime']; 
$prezime=$_POST['prezime']; 
$password_hash=md5($password); 

if(!empty($password) && !empty($password_again)&& !empty($username)&& !empty($ime)&& !empty($prezime)){ 

if($password!=$password_again){ 

echo '<div class="text2">Passwordi se ne poklapaju :)</div>'; 

}else{ 

$query="SELECT username FROM studenti WHERE username='$username'"; 
$query_run = mysql_query($query); 

if(mysql_num_rows($query_run)==1){ 

echo '<div class="text2">Korisnicko ime'.' '.$username.' '.'vec postoji</div>'; 
}else{ 
$query="INSERT INTO studenti VALUES ('','".mysql_real_escape_string($username)."','".mysql_real_escape_string($password_hash)."','".mysql_real_escape_string($ime)."','".mysql_real_escape_string($prezime)."')"; 
if($query_run=mysql_query($query)){ 

header('Location: uspjeh.php'); 

}else{ 
    echo '<div class="text2">Dogodila se greska, probaj kasnije!</div>'; 
} 
} 
} 
}else{ 

    echo '<div class="text4">Popuni sva polja</div>'; 
} 
} 
    ?> 

這是我在pastebin 我的服務器使用的PHP版本5.3.26的所有代碼,所以我不需要使用mysqli_connect();

+1

不要使用MD5進行散列密碼。它壞了,不安全。 –

+1

你做了什麼來解決這個問題?這裏有很多代碼讓我覺得你還沒有做足夠的調試。 –

+1

那你的[session_start()](http://www.php.net/manual/en/function.session-start.php)在哪裏? –

回答

0

您在第一個文件(require/include)中缺少connectdb.php!也許你沒有數據庫連接。

您必須在每個使用$_SESSION[]變量的文件的開始處設置session_start()

而你應該切換到mysqliPDO,因爲mysql已被棄用,並且會在新版本的PHP中被刪除。

mysql_real_escape_string()你只有基本的安全。看看prepared statements (mysqli)/prepared statements (PDO)

而且您必須在每個查詢中都轉義值,包括「SELECT」。 (例如,$username

+0

好的,這裏是我的所有代碼..你可以請看看它.. http://pastebin.com/R7tyvCXZ所有作品..只是..這個問題是非常wirde .. – Smack