2015-08-23 80 views
0

我有我的登錄部分驗證碼:我如何讓PHP登錄?

<form method="post" action="loginprocess.php"> 
    <div class="row"> 
     <div class="6u 12u$(mobile)"><input type="text" name="username" placeholder="Usuario" /></div> 
     <div class="6u$ 12u$(mobile)"><input type="password" name="pass" placeholder="Contraseña" /></div> 
     <div class="12u$"> 
      <input type="submit" value="Iniciar sesion" /> 
     </div> 
    </div> 
</form> 

這裏有我的註冊代碼:

<?php 
    include"config.php"; 
    $username = $_POST['username']; 
    $email = $_POST['email']; 
    $pass  = md5($_POST['pass']); 
    $rpass = md5($_POST['rpass']); 
    $reqlen = strlen($username) * strlen($email) *strlen($pass) * strlen($rpass); 
    $insert = 'INSERT INTO registro (username, email, pass) VALUES ("'.$username.'", "'.$email.'", "'.$pass.'")'; 
    $query = mysql_query("SELECT * FROM registro WHERE username='$username'"); 
    $query2 = mysql_query("SELECT * FROM registro WHERE email='$email'"); 

    if(mysql_num_rows($query) > 0) 
    { 
     header("Location: registrate.php?error1"); 
     exit; 
    } 
    else 
    { 
     if(mysql_num_rows($query2) > 0) 
     { 
     header("Location: registrate.php?error2"); 
     exit; 
     } 
    } 
    if ($reqlen > 0) 
    { 
     if ($pass === $rpass) 
     { 
      mysql_query($insert); 
      header("Location: index.php"); 
      exit; 
     } 
     else 
     { 
      header("Location: registrate.php?error3"); 
     } 
    } 
    else 
    { 
     header("Location: registrate.php?error4"); 
    } 
?> 

在這裏,我連接到服務器:

<?php 
    $localhost = "localhost"; 
    $dbuser  = "root"; 
    $dbpass  = "admin"; 
    $dbname  = "vivejugando"; 

    $connect = mysql_connect($localhost, $dbuser, $dbpass) or die("No se pudo conectar a la base de datos"); 
    mysql_select_db("$dbname", $connect); 
?> 

我需要登錄代碼。我做了一些類似這樣的,但不工作:

<?php 
    include"config.php" 
    $username = $_POST ['username']; 
    $pass  = md5($_POST['pass']); 
    $query  = mysql_query("SELECT * FROM registro WHERE username='$username' AND pass='$pass'"); 
    $data  = mysql_fetch_assoc($query); 

    if(mysql_num_rows($query)) 
    { 
     session_start(); 
     $_SESSION['username'] = $data['username']; 
     header("Location: members.php"); 
     exit; 
    } 
    header(""Location: regprocess.php"") 
?> 

如果有人提高了我的登錄代碼或使其優化,以我的代碼,我欣賞了這麼多。

+2

'不work'手段?請提及具體的錯誤,以便這裏的開發人員可以快速解決您的問題。 – Jigar

+0

請指定錯誤。在mysql_query之後使用'或死(mysql_error())'。事實上,你不應該使用mysql_的東西,因爲它們已被棄用。 –

回答

1

試試吧

include"config.php"; 
$username = $_POST['username']; 
$email = $_POST['email']; 
$pass  = md5($_POST['pass']); 
$rpass = md5($_POST['rpass']); 

if(isset($rpass) && $pass == $rpass) 
{ 
    $insert = 'INSERT INTO registro (username, email, pass) VALUES ("'.$username.'", "'.$email.'", "'.$pass.'")'; 
    mysql_query($insert); 
    session_start(); 
    $_SESSION['username'] = $username; 
    header("Location: index.php"); 
    exit; 
} 

$check_login = mysql_query("SELECT * FROM registro WHERE username='$username' and email='$email'");  
if(mysql_num_rows($check_login) > 0) 
{ 
    header("Location: index.php"); 
    exit; 
} 
else 
{ 
    header("Location: registrate.php?error1"); 
    exit; 
}