簡體   English   中英

PHP的奇怪的錯誤,如果?

[英]php strange error with if?

你好,我正在嘗試創建一個帳戶激活頁面,但是當我嘗試激活一個帳戶時,它可以工作兩次。 所以基本上我的激活鏈接使用的用戶ID是使用base64_encode()編碼的,而用戶密碼是使用crypt(sha512)編碼的。 所以我在注冊頁面上的代碼如下所示:

    $qry = "SELECT * FROM users WHERE username='$username'"; 
    $res = mysql_query($qry);
    $row = mysql_fetch_row($res);
    $userid=$row[0];//gets the id of the user
    $userpass=$row[2];//gets the pass from database (which is already encoded)
    $userid=base64_encode($userid); //encodes userid
    $code=substr($userpass,6,strlen($userpass)-6); // cuts off some $6$xx$ information which is needed for crypt.
    $message="//here is some message and then the link 
    http://www.xxx.be/forum/confirm.php?userid=".$userid."&code=".$code;

    mail($email , "xxx registration confirmation" ,$message,"From:NoReply@xxx.be");

這是我在confirm.php中使用的代碼:

$userid=base64_decode($_GET['userid']); 
$qry = "SELECT * FROM users WHERE id='$userid'"; 
$res = mysql_query($qry);
$row = mysql_fetch_row($res);
if ($userid%2==0) {
$pass=substr($row[2],0,strlen($row[2])-1);
} else {
$pass=$row[2];
}
if ($pass=="$6$10$".$_GET['code']) {
$qry = "UPDATE users SET activated=1
WHERE id=$userid"; 
$res = mysql_query($qry);

所以這是我的問題:(confirm.php中的5-9行)我不明白為什么我必須這樣做。 每次我創建一個帳戶,只有當用戶名是奇數時,它才起作用。 如果是的話,它還會在密碼上添加一個圓點。 所以像這樣:
用戶名:1​​密碼:某物
用戶名:2密碼:stackoverflow。

因此,這就是為什么$ pass不會處理“ $ 6 $ 10 $”。$ _ GET ['code']而整個代碼失敗的原因。 我完全不知道為什么當我的用戶ID為0時為什么要添加點。ps:confirm.php中的5-9行解決了該問題。 但我只想知道為什么會這樣。

編輯:請在回答前閱讀全文。

if ($userid%2==0) {
    $pass=substr($row[2],0,strlen($row[2])-1);
} else {
    $pass=$row[2];
}

該語句是無用的,它正在破壞您的代碼。 如果userid ==偶數,則它切斷$ row [2]字符串中的最后一個字符。

要解決此問題,您可以刪除if語句並使用

$pass = $row[2];

另外,正如旁人所述,您應該考慮使用准備好的語句來防止SQL注入。 或者至少在將它們放入查詢之前,先清理使用的$ _GET變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM