0

ビジネス用にブログ タイプのページを設定しています。MySQL と PHP の真新しい。このログイン システムをセットアップします。何らかの理由で、ログインがドロップする理由がわかりません。エラーをチェックし、電子メールとパスワードが正しい場合、php を介して 'good' を返すとします。php が正常に返された場合は、ブログ ページにリダイレクトされます。

数か月間これに対処してきましたが、必死の助けが必要です。ありがとうございました。これは、jquery に付随する php コードです。

テストサイトへのリンクはこちらです。 test.toddprod.com/login 本当に助かります。ありがとう

<?php
#fake mysql connection first
DEFINE ('DB_USER','usernamegoeshere');
DEFINE ('DB_PASSWORD','passwordhere');
DEFINE ('DB_HOST','hostnamehere');
DEFINE ('DB_NAME','andtheotherthinghere');

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL');

$db = mysql_select_db(DB_NAME, $dbc) or die('Could not select database.'.mysql_error());


$e = $_POST['email'];
$pass = $_POST['pass'];
$q = 'SELECT user_id from toddprod where email="'.$e.'" and pass= SHA1("'.$pass.'")';
$r = mysql_query($db, $q);
if( mysql_num_rows($r)==1 ){
    setcookie ( 'user_id', $r);
    setcookie ( 'email', '$e');
    setcookie ( 'logged-in', 'true');
    echo 'good';
    }
else if (mysql_num_rows($r)==0) {
    echo 'Your '.$e.' with password '.$pass;
};
mysql_close ($db);
?>
4

2 に答える 2

1

うーん、ここで間違っていると思うことがたくさんあります...

まず、クエリをサニタイズする必要があります...

$email = mysql_real_escape_string ($_POST['email']); // escape the email
$pass = SHA1(mysql_real_escape_string ($_POST['pass'])); // escape and encrypt the pass

// now you can put it into the query safely
$query = "SELECT user_id from toddprod where email = '$email' and pass = '$pass' ";

次に、クエリを間違って実行すると、mysql_query関数はクエリとデータベース接続の2つの引数を取ります。間違った引数を渡しています。クエリと、mysql_select_dbブール値である関数の結果を渡しています。したがって、そのクエリに参加する必要はあり$dbcません$db。その場合でも、引数を間違った順序で渡します。接続よりもクエリが最初に実行されます。だからそれは...

$result = mysql_query($query, $dbc);

次に、関数からの戻り値をCookieとして設定しようとしてmysql_queryいますが、その値はリソースであり、必要なユーザーIDではありません。このように、実際にリソースから値を読み取る必要があります。

$row = mysql_fetch_array($result);
$userid = $row["user_id"];
setcookie('user_id', $userid);

次に進みます...電子メールCookieを設定する場合、変数は一重引用符で$e囲まれているため、一重引用符は文字列litterlyを(変数を解析せずに)格納するため、Cookieには実際の電子メールではなく実際に含まれます。したがって、二重引用符を使用するか、引用符をまったく使用しないでください。したがって、次のいずれかで問題ありません...

setcookie('email', "$e");
setcookie('email', $e);

最後になりましたが、ifステートメントの最後にセミコロンを付けないでください。また、データベース選択の結果ではなく接続をmysql_close関数に渡す必要があるため、次のようにする必要があります。

mysql_close($dbc);

そこで、これがあなたをどこかに連れて行ってくれることを願っています。これらの変更を試してみてください。問題が解決しない場合は、さらにサポートさせていただきます。

ここにあなたを助けるリンクがあります:

http://www.php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-fetch-array.php

http://www.php.net/manual/en/function.mysql-real-escape-string.php

編集:

ここでは、見つけた問題に応じてコードを修正しました。試してみてください。テストできなかったので、あちこちで小さな構文エラーが発生する可能性がありますが、比較できるものが得られるはずです。また、将来的には、変数に意味的/適切な名前を付けて、他の人が簡単に取得できるようにし、いくつかの関数に$dbcではなく$dbを渡すように混乱しないようにすることをお勧めします。

<?php
    // keep the function names in lowercase, no reason, just looks better to me
define('DB_USER', 'usernamegoeshere');
define('DB_PASSWORD', 'passwordhere');
define('DB_HOST', 'hostnamehere');
define('DB_NAME', 'andtheotherthinghere');

    // connect to the mysql server
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL');
    // select the database, you don't need to store the result, it just returns true or false
mysql_select_db(DB_NAME, $conn) or die('Could not select database.' .mysql_error());

    // escape the input
$email = mysql_real_escape_string($_POST['email']);
$pass = sha1(mysql_real_escape_string($_POST['pass']));

    // create the query
$query = "SELECT user_id FROM toddprod WHERE email = '$email' AND pass = '$pass'";

    // execute the query
$result = mysql_query($query, $conn);
$usercount = mysql_num_rows($result);

if($usercount == 1){
    // read the results and get the user_id
    $row = mysql_fetch_array($result);
    $userid = $row['user_id'];

    // set the cookies
    setcookie('user_id', $userid);
    setcookie('email', $email);
    setcookie('logged-in', 'true');

    // echo success message
    echo 'good';
}elseif($usercount == 0) {
    echo "You're $email with password $pass";
}
mysql_close($conn);
?>
于 2011-09-08T01:02:10.557 に答える