2017-11-11 239 views
0
<?PHP 
require_once('connect.php'); 
if(isset($_POST) & !empty($_POST)){ 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    $sql = "INSERT INTO login (username, password) VALUES ('$username', 
'$password')"; 
    $result = mysqli_query($connection, $sql); 

    if ($result){ 
    $smsg = "User Registration successful, Redirecting to Login."; 
    } else { 
    $fmsg = "User Registation failed"; 
    } 
} 
?> 

    <label for="inputEmail" class="sr-only">Email address</label> 
    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address"> 

    <label for="inputPassword" class="sr-only">Password</label> 
    <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password"> 

    <label for="inputPassword" class="sr-only">Password</label> 
    <input type="password" name="cpassword" id="inputPassword" class="form-control" placeholder="Confirm password"> 
    <div id="Passwordsmatch"> 
    </div> 

    <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button> 
    <br> 
    <p class="haveanacc">Already have an account? <a href="login.php">Login</a></p> 
    </form> 
</div> 

我需要密碼驗證幫助確保密碼=== confirmpassword。 如何使密碼字段必須等於確認密碼字段,否則$ fmsg/echo密碼不匹配。確認密碼PHP

嘗試了很多不同的東西似乎沒有任何工作。我對PHP比較陌生。

+0

請出示你的東西嘗試請。也許離工作還有點距離,那麼我們可以解決它,並且證明你在請求其他人解決問題之前做出了努力。 – ADyson

回答

-1

添加一個字段con_password。密碼確認

if(isset($_POST)){ 
$username = $_POST['username']; 
$password = $_POST['password']; 
$con_password = $_POST['con_password']; 
if($password == $con_password){ 
$sql = "INSERT INTO login (username, password) VALUES ('$username', 
'$password')"; 
$result = mysqli_query($connection, $sql); 
if ($result){ 
$smsg = "User Registration successful, Redirecting to Login."; 
} else { 
$fmsg = "User Registation failed";` 
} 
}} 
+0

我一直在收取$ fmsg =「用戶註冊失敗」;即使在密碼匹配或不匹配的情況下,我也會盡力嘗試 – Lucasirusta

+0

1.它是代碼中的sql注入2.不能存儲純文本密碼。 – zerkms

+0

請記住,確認字段在HTML代碼中稱爲'cpassword',而不是'con_password'。 – Progman

0

請注意語法。

if ($a & $b) { 
// is not equal to 
if ($a && $b) 

// notice && vs & 

首先,您需要清理您的用戶輸入。你在那裏做的事情可能很容易出現sql注入安全問題。有些工具可以爲用戶輸入提供清理,但對於這個小例子,假設您只需要用戶名和密碼的字母數字字符。 您還可以在這裏http://docs.kisphp.net/database-connect/本教程和使用OOP

無特殊字符

<?php 
// rest of the code here 
function sanitize($input) // this should be in a different file 
{ 
    return preg_replace('/([^a-zA-Z0-9]+)/', '', $input); 
} 

$username = sanitize($_POST['username']); 
$password = sanitize($_POST['password']); 
$password_confirm = sanitize($_POST['password_confirm']); 

if (strcmp($password, $password_confirm) !== 0) { 
    die('Password do not match'); 
    // or add the message in flash session and redirect user 
} 

// if everything is fine 
$sql = "INSERT INTO users SET "; 
$sql .= sprintf("username = '%s', ", $username); 
$sql .= sprintf("password = '%s'", md5($password)); 
// instead of md5 you can use some other algorithm 

$result = mysqli_query($mysqli_connection, $sql) or die(mysqli_error($mysqli_connection)); 

if (mysqli_insert_id($mysqli_connection) > 0) { 
    echo "Success"; 
    // make redirect here 
} else { 
    // display error here 
} 

我建議你使用OOP儘可能。有ORM在那裏,你可以使用成功,並會提供比平原PHP更多的反饋意見。如果您不知道如何操作,請按照上述網址中的教程進行操作,這會讓您更清楚。

P.S.我不使用程序方法,因此如果您複製粘貼此代碼,則可能會出現一些錯誤。

PPS

我看到很多的例子與比較是這樣的:

if ($a == $b) 

千萬不要這麼做

如果你運行這段代碼:

if ($any_string == 0) { 
    echo "this will always be shown, no matter what you have in variable"; 
} 

使用strcmp===

1

試試這個:

<?PHP 
require_once('connect.php'); 
if(isset($_POST) & !empty($_POST)){ 
$username = $_POST['username']; 
$password = $_POST['password']; 

之後再加入

$cpassword = $_POST['cpassword']; 
if($password == $capssword){ 

...... Your rest of the code 

}