2017-06-21 8 views
-2

これで、PHPを書く方法を学んでいます。私は小さなショッピングサイトを作りたいと思っています。私のindex.htmlは次のようなものになります。私たちが初めてアップロードするページのPHPコードセクションをスキップ

<!DOCTYPE html> 
<html> 
    <head> 
    <link href="index.css" rel="stylesheet" /> 
     <title> 
     eShop 
     </title> 
    </head> 
    <body> 
    <div class="topnav"> 
     <a class="active" href="#index.html">Home</a> 
     <a href="loginAdmin.php">Administrator</a> 
     <a href="loginUser.php">Register User</a> 
     <a href="newAccount.php">Register New Account</a> 
    </div> 
    <img class="centerImage" src="eshop.jpg"> 
</body> 
</html> 

をしてloginAdmin.phpファイルは次のようになります。PHPコードのでloginAdminリンク上で押されたユーザーが実行された場合

<?php 
session_start(); 
// here is the code that connects to the database. Note that the username 
// and password are "hard-coded". 
$user="root"; 
$passwd=""; 
$database=""; 

$link = mysqli_connect(localhost,$user,$passwd); 
@mysqli_select_db($link,$database) or die ("Unable to select database"); 

// try to create a new record from the submission 
$username = mysqli_real_escape_string($link,$_REQUEST['username']); 
$password= mysqli_real_escape_string($link,$_REQUEST['password']); 

if ($username && $password) { 

    // here we define the SQL command 
    $query = "SELECT * FROM people WHERE Username='$username' AND Password='$password'"; 

    // submit the query to the database 
    $res=mysqli_query($query); 

    // make sure it worked! 
    if (!$res) { 
    echo mysql_error(); 
    exit; 
    } 

    // find out how many records we got 
    $num = mysqli_numrows($res); 

    if ($num==0) { 
    echo "<h3>Invalid login</h3>\n"; 
    exit; 
    } elseif ($num!=1) { 
    echo "<h3>Error - unexpected result!\n"; 
    exit; 
    } 

    // valid login, set the session variable 
    $_SESSION['userid']=mysql_result($res,0,'userid'); 
    echo "<h3>Welcome $username</h3>\n"; 
?> 

<head> 
    <link href="login.css" rel="stylesheet" /> 
    <title> 
     eShop 
    </title> 
</head> 

<body> 
    <div class="login-page"> 
     <div class="form"> 
      <form class="login-form"> 
       <input type="text" placeholder="User Name:" /> 
       <input type="password" placeholder="Password:" /> 
       <button onclick="writeMsg()">login</button> 
      </form> 
     </div> 
    </div> 
</body> 

、および私は望んでいない、ユーザーがログインボタンを押した後にのみ、私はPHPコードブロックが実行されたいです。どうやってやるの?たぶん私はファイル(phpとhtml)とindex.htmlのPHPファイルのユーザーhrefを分離する必要がありますか? index.htmlファイルはindex.phpにする必要がありますか?

+0

'$ _POST ['username']'(または$ _REQUEST、しかし私はpreciceが好きです)が設定されているかどうかを確認する必要があります( 'isset($ _ POST ['username'] ') - >必要なものを実行してください。それ以外の場合は、認証されたユーザーのためにセッションを使用して、適切な方法で – Jeff

+0

フォームを表示してください。より多くのコンテンツを表示するためにユーザーセッションが存在するかどうかを判断します。 –

+0

[Little Bobby](http://bobby-tables.com/)によると*** [あなたのスクリプトはSQLインジェクション攻撃の危険にさらされています。](http://stackoverflow.com/questions/60174/how-can- i-prevent-sql-injection-in-php)*** [MySQLi](http://php.net/manual)の[prepared](http://en.wikipedia.org/wiki/Prepared_statement)ステートメントについて学びます。 /en/mysqli.quickstart.prepared-statements.php)。 [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)でも安全ではありません! –

答えて

-1

フォーム提出が発生したときに条件を満たす条件でPHPコードを追加する必要があります。また、あなたはあなたの入力フィールド

に名前を追加する必要があなたのコードは次のようになります

、私はちょうど&は、条件内のすべてのPHPコードを保ったフォームフィールドに名前を追加した

<?php 
session_start(); 
if(isset($_POST['username']) && isset($_POST['password'])) { //Added this line 
    // here is the code that connects to the database. Note that the username 
    // and password are "hard-coded". 
    $user="root"; 
    $passwd=""; 
    $database=""; 

    $link = mysqli_connect(localhost,$user,$passwd); 
    @mysqli_select_db($link,$database) or die ("Unable to select database"); 

    // try to create a new record from the submission 
    $username = mysqli_real_escape_string($link,$_REQUEST['username']); 
    $password= mysqli_real_escape_string($link,$_REQUEST['password']); 

    if ($username && $password) { 

     // here we define the SQL command 
     $query = "SELECT * FROM people WHERE Username='$username' AND Password='$password'"; 

     // submit the query to the database 
     $res=mysqli_query($query); 

     // make sure it worked! 
     if (!$res) { 
      echo mysql_error(); 
      exit; 
     } 

     // find out how many records we got 
     $num = mysqli_numrows($res); 

     if ($num==0) { 
      echo "<h3>Invalid login</h3>\n"; 
      exit; 
     } elseif ($num!=1) { 
      echo "<h3>Error - unexpected result!\n"; 
      exit; 
     } 

     // valid login, set the session variable 
     $_SESSION['userid']=mysql_result($res,0,'userid'); 
     echo "<h3>Welcome $username</h3>\n"; 
    } 
} //Added this line 
?> 

<head> 
    <link href="login.css" rel="stylesheet" /> 
    <title> 
     eShop 
    </title> 
</head> 

<body> 
    <div class="login-page"> 
     <div class="form"> 
      <form class="login-form" method="POST"> <!-- edited this line --> 
       <input type="text" name="username" placeholder="User Name:" /> <!-- edited this line --> 
       <input type="password" name="password" placeholder="Password:" /> <!-- edited this line --> 
       <button onclick="writeMsg()">login</button> 
      </form> 
     </div> 
    </div> 
</body> 

+1

潮を回して、冗長で危険なコーディングを教える/伝播させないようにします。準備ができていない文章を投稿した場合[投稿する前にこれを考慮する](http://meta.stackoverflow.com/q/344703/)。さらに[もっと貴重な答えは、正しい方法をOPに示すことから来ている](https://meta.stackoverflow.com/a/290789/1011527)。 –

+0

この回答には5つのエラーがあります。あなたがHTMLマークアップの外にechoされたものを数えれば6。 –

+0

@JayBlanchard、OPは初心者なので、あまりにも多くのものを入れたくありませんでした。しかし、はい、私は私の答えを更新し、また私の将来の答えであなたのメモを考慮します。とにかく言及いただきありがとうございます。 – manian

関連する問題