2016-05-05 60 views
0

通常我只會使用基本的PHP和MySQL以及一些簡單的CSS風格。然而,我喜歡Bootstrap框架的外觀和感覺,並且希望將它融入到我的PHP中,但我是一個相對新手,從哪裏開始。我想從一個簡單的實踐例子開始。爲了使用Bootstrap來創建樣式,我需要對以下代碼進行哪些更改?

使用下面的代碼,這是一個使用PHP和MySQL的簡單登錄腳本,爲了使用Bootstrap,需要進行哪些更改?

我已經下載的引導文件..

<?php 
$connect = mysqli_connect("db location","username","password", "forks") or die(mysql_error()); 


if(isset($_COOKIE['ID_your_site'])){ //if there is, it logs you in and directes you to the members page 
    $username = $_COOKIE['ID_site']; 
    $pass = $_COOKIE['Key_site']; 
    $check = mysqli_query($conect, "SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 

    while($info = mysqli_fetch_array($check)){ 
     if ($pass != $info['password']){} 
     else{ 
      header("Location: login.php"); 
     } 
    } 
} 

//if the login form is submitted 
if (isset($_POST['submit'])) { 

    // makes sure they filled it in 
    if(!$_POST['username']){ 
     die('You did not fill in a username.'); 
    } 
    if(!$_POST['pass']){ 
     die('You did not fill in a password.'); 
    } 

    // checks it against the database 
    if (!get_magic_quotes_gpc()){ 
     $_POST['email'] = addslashes($_POST['email']); 
    } 

    $check = mysqli_query($conect, "SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error()); 

//Gives error if user dosen't exist 
$check2 = mysqli_num_rows($check); 
if ($check2 == 0){ 
    die('That user does not exist in our database.<br /><br />If you think this is wrong <a href="login.php">try again</a>.'); 
} 

while($info = mysqli_fetch_array($check)){ 
    $_POST['pass'] = stripslashes($_POST['pass']); 
    $info['password'] = stripslashes($info['password']); 
    $_POST['pass'] = md5($_POST['pass']); 

    //gives error if the password is wrong 
    if ($_POST['pass'] != $info['password']){ 
     die('Incorrect password, please <a href="login.php">try again</a>.'); 
    } 

    else{ // if login is ok then we add a cookie 
     $_POST['username'] = stripslashes($_POST['username']); 
     $hour = time() + 3600; 
     setcookie(ID_your_site, $_POST['username'], $hour); 
     setcookie(Key_your_site, $_POST['pass'], $hour);  

     //then redirect them to the members area 
     header("Location: members.php"); 
    } 
} 
} 
else{ 
// if they are not logged in 
?> 

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2><h1>Login</h1></td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40"> 
</td></tr> 
<tr><td>Password:</td><td> 
<input type="password" name="pass" maxlength="50"> 
</td></tr> 

<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form> 

<?php 
} 
?> 

任何幫助,將不勝感激。

+0

你真的不應該使用[MD5密碼哈希](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure),你真的應該使用PHP的[內置函數](http: //jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼秒urity。確保你[不要逃避密碼](http://stackoverflow.com/q/36628418/1011527)或在哈希之前使用其他任何清理機制。這樣做會改變密碼並導致不必要的附加編碼。 –

+0

[Little Bobby](http://bobby-tables.com/)說[你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent -sql -injection-in-php)瞭解[MySQLi]的[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句(http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

回答

-3

Appart酒店從安全問題的腳本中,你可以檢查出:https://getbootstrap.com/examples/signin/

,如果你看看源,你會看到熟悉的<形式>

來源:

<!-- here your php code --> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <title>Signin Template for Bootstrap</title> 

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 

    </head> 

    <body> 

    <div class="container"> 

     <form class="form-signin" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
     <h2 class="form-signin-heading">Please sign in</h2> 
     <label for="inputEmail" class="sr-only">Email address</label> 
     <input type="email" id="inputEmail" class="form-control" placeholder="Email address" name="username" required autofocus> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input type="password" id="inputPassword" name="pass" class="form-control" placeholder="Password" required> 
     <div class="checkbox"> 
      <label> 
      <input type="checkbox" value="remember-me"> Remember me 
      </label> 
     </div> 
     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
     </form> 

    </div> 
    </body> 
</html> 
+0

*「熟悉的......」*? –

+0

@JayBlanchard熟悉的

(參見[source](http://stackoverflow.com/revisions/0c799362-ef5b-4789-9ce4-00fd2ef1939a/view-source)) –

相關問題