如何在用户注册系统时进行登录

时间:2014-09-27 08:53:31

标签: php mysql

目前,在我的登录系统中,用户必须在注册后登录。

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p>Not a member yet?.Sign up here.By Sign up You are accepting our <a href="terms.php">terms and conditions</a> </p>
    <fieldset>
      <legend>Registration Info</legend>
      <label for="username">Username:</label>
      <input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />
      <label for="email">Email id:</label>
      <input type="email" id="email" name="email" /><br />
      <label for="password1">Password:</label>
      <input type="password" id="password1" name="password1" /><br />
      <label for="password2">Password (retype):</label>
      <input type="password" id="password2" name="password2" /><br />
    </fieldset>
    <input type="submit" value="Sign Up" name="create" />
  </form>

如果表单提交正确

<?php
      if (!empty($user_username) && !empty($user_password)) {
 // Make sure someone isn't already registered using this username
  $query = "SELECT * FROM <database> WHERE username = '$username'";
  $data = mysqli_query($dbc, $query);
  if (mysqli_num_rows($data) == 0) {
    // The username is unique, so insert the data into the database
    $query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
    $result=mysqli_query($dbc, $query) or die("cant create an account");
    }else{
        die("Somthing went wrong");
    }
      }
?>

一旦完成,他必须进入登录页面并使用以下代码重新登录以登录

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
      <legend>Log In</legend>
      <label for="username">Username:</label>
      <input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
      <label for="password">Password:</label>
      <input type="password" name="password" />
    </fieldset>
    <input type="submit" value="Log In" name="submit" />
  </form>

当用户注册该时间本身用户必须被带到主页时,我不希望这个系统发生这样的事情。并且在注册过程中也必须设置会话变量。

2 个答案:

答案 0 :(得分:0)

尝试重用代码,它将有助于管理您的代码和应用程​​序,请为登录过程创建一个函数,并使用相同的函数进行正常登录,然后在注册后使用用于注册的详细信息调用相同的函数。

function authenticate($user_id, $pwd)
{
 // do the logic for login, 
 //if success then set value in session and  redirect to the respective page
 //if failed then error page
}



  if (!empty($user_username) && !empty($user_password)) {
 // Make sure someone isn't already registered using this username
  $query = "SELECT * FROM <database> WHERE username = '$username'";
  $data = mysqli_query($dbc, $query);
  if (mysqli_num_rows($data) == 0) {
    // The username is unique, so insert the data into the database
    $query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
    $result=mysqli_query($dbc, $query) or die("cant create an account");
    // call the authenticate function for login
    authenticate($username, SHA('$password1'));
    }else{
        die("Somthing went wrong");
    }
      }

答案 1 :(得分:0)

尝试这样的事情: -

<?php
      if (!empty($user_username) && !empty($user_password)) {
 // Make sure someone isn't already registered using this username
  $query = "SELECT * FROM <database> WHERE username = '$username'";
  $data = mysqli_query($dbc, $query);
  if (mysqli_num_rows($data) == 0) {
    // The username is unique, so insert the data into the database
    $query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
    $result=mysqli_query($dbc, $query) or die("cant create an account");

    $_SESSION["username"] = $username; // assign session variable here.
    header("Location: home.php");  // redirect to home page

    }else{
        die("Somthing went wrong");
    }
      }
?>