I need a PHP mySQL Login script that directs user to specific URL based on username and password

大城市里の小女人 提交于 2019-12-08 08:14:55

问题


Can anyone show me or point me in a direction that shows to assign a specific URL to a user based on their Username and Password?

Erik


回答1:


Why don't you put that URL in the database associated with that user in question. If the query for the user and password are TRUE, then redirect to that URL associated to that user.

Here's an example, let says that you have 1 user in the database called "users" with the fields:

  • id (Id with autoincrement and set as Primary key)
  • user (Username)
  • pass (Password)
  • url (The url for the redirection)

And the login.php form is like

    <?php
    //Database connection
    $sql = mysql_connect("localhost","dbUsername","dbPassword");
    mysql_select_db("dbName", $sql);

    if ($_POST['submitLogin'){
     //Login query
     $q = mysql_query("SELECT * FROM users WHERE user LIKE '".$_POST['user']."' AND pass LIKE '".$_POST['pass']."'");
     $r = mysql_fetch_assoc($q);
     if (mysql_num_rows($q) >= 1){
       //If the user / password are found -> redirect
       header('Location:'.$r['url']);
     }else{
       //else return the login error
       echo "Login failed";
    }
    ?>


    <form action="" method="post">
     User : <input type="text" name="user" /><br>
     Password : <input type="password" name="pass"/><br>
     <input type="submit" name="submitLogin" value="Login"/>
    </form>

I think this should do the trick :D




回答2:


The way you'd send the query and parse the result would depend on whether you're using mysqli, PDO, or something else to access your database. Lets say you're using mysqli.

Assuming the database connection is referenced in $dbc and you've got your username and password in $user and $pass, and have validated them, and sanitized them for use in a query, you could do something like:

$query = "SELECT url FROM table_name WHERE user = $user AND pass = $pass";
$result = @mysqli_query ($dbc, $query);

if ($row = mysqli_fetch_assoc($result)){
    header ('Location: ' . $row['url']);
    exit;
}
else{

    // Handle invalid username/password

}

Very basic, and you'd use SHA1 or whatever encryption you're using to store passwords in the query.



来源:https://stackoverflow.com/questions/6805008/i-need-a-php-mysql-login-script-that-directs-user-to-specific-url-based-on-usern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!