url重定向的用户名

时间:2015-01-27 12:33:09

标签: php mysql redirect server-side username

我有一个基本系统安装了一个应该读取用户名和网址的表,但我是一个完整的菜鸟,当涉及到PHP这是我有的,如果有人可以帮助我,以便当用户名自动输入时重定向他们与该帐户相关联的网址,谢谢。

   <?php 
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="redirect"; // Table name 

    // Connect to server and select databse. 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 

    // username and password sent from form 
    $myusername=$_POST['myusername']; 

    // To protect MySQL injection (more detail about MySQL injection) 
    $myusername = stripslashes($myusername); 

    $myusername = mysql_real_escape_string($myusername); 
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername'"; 
    $result=mysql_query($sql); 

    // Mysql_num_row is counting table row 
    $count=mysql_num_rows($result); 
    // If result matched $myusername and $mypassword, table row must be 1 row 
    if($count==1){ 
    // Register $myusername, $mypassword and redirect to file"login_success.php" 
    $_SESSION['username'] = $myusername;  
    $result = mysql_query("SELECT redirect FROM members"); 

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
       header("Location: " . $row['redirect']); 
    } 
    exit(); 
    } 
    else { 
    echo "Wrong Username or Password"; 
    } 
    ?> 

**Edit:**
Quick Update basically this has a text entry box which when filled with the correct phrase of password it will redirect you to a external url which is saved with the name on a database. I dont know how to get it so that the redirect happens properly from a text box if anyone has a tutorials on this id enjoy it

感谢大家的帮助!

固定代码工作。         

        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password="root"; // Mysql password 
        $db_name="test"; // Database name 
        $tbl_name="redirect"; // Table name 

        // Connect to server and select databse. 
        $con = mysqli_connect($host, $username, $password, $db_name);

        // username sent from the form 
        $username=$_POST['username']; 
        // To protect MySQL injection (more detail about MySQL injection) 
        $username = stripslashes($username); 

        $sql = "SELECT * FROM redirect WHERE username='$username' LIMIT 1"; 

        $result = mysqli_query($con, $sql);
        // Mysqli_num_row is counting table row 
        $count = mysqli_num_rows($result);
        // If result matched $myusername, table row must be 1 row 
        if($count === 1){
            $_SESSION['username'] = $username; 
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);   
            //redirect to file url from database
            header("Location: " . $row['url']); 
        }else{
            echo 'Invalid Username';
        }

    }else{
?>  

    <html lang="en">
    <head>
        <title>My App</title>
    </head>
    <body>
        <form action="redirect.php" method="post">
            <input type="text" name="username"/>
            <input type="submit" value="submit"/>
        </form>
    </body>
    </html>
<?php } ?> 

3 个答案:

答案 0 :(得分:0)

if(isset($_SESSION['username'])){
    header("location: http://yoururl/" .$_SESSION['username']);
}

答案 1 :(得分:0)

 <?php
    if($_POST){

        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password="root"; // Mysql password 
        $db_name="test"; // Database name 
        $tbl_name="redirect"; // Table name 

        // Connect to server and select databse. 
        $con = mysqli_connect($host, $username, $password, $db_name);

        // username sent from the form 
        $myusername = $_POST['myusername']; 
        // To protect MySQL injection (more detail about MySQL injection) 
        $myusername = stripslashes($myusername); 
        $sql = "SELECT * FROM redirect WHERE username='$myusername' LIMIT 1"; 

        $result = mysqli_query($con, $sql);
        // Mysqli_num_row is counting table row 
        $count = mysqli_num_rows($result);
        // If result matched $myusername, table row must be 1 row 
        if($count === 1){
            $_SESSION['username'] = $myusername; 
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);   
            //redirect to file url from database
            header("Location: " . $row['url']); 
        }else{
            echo 'Invalid Username';
        }
    }else{
?>  

    <html lang="en">
    <head>
        <title>My App</title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
            <input type="text" name="myusername"/>
            <input type="submit" value="submit"/>
        </form>
    </body>
    </html>
<?php } ?>  

这应该可以解决...

答案 2 :(得分:0)

将此替换为你的。

$result = mysql_query("SELECT redirect FROM members where username = '.$myusername.'");