1

I have a base system installed with a table that should read a username and url but i am a complete noob when it comes to PHP this is what ive got if someone could help me out so that when the username is entered it automatically redirects them the url associated with that account thanks.

 <?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

Dharman
  • 26,923
  • 21
  • 73
  • 125
albaslayer
  • 33
  • 6
  • Are you using 2 tables or single 1? – adarsh hota Jan 27 '15 at 12:41
  • 1 single its a table with just usernames and redirects – albaslayer Jan 27 '15 at 12:44
  • Basically I have 1 domain i want them to type in a code and get redirected to a subdomain but there will be a massive list of them – albaslayer Jan 27 '15 at 12:44
  • 1
    **Important Note:** Never use mysql_* functions. They are [deprecated](http://php.net/manual/en/migration55.deprecated.php#migration55.deprecated.mysql). Use `mysqli` or `PDO` instead. See [this](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) question. – Anoop S Jan 27 '15 at 13:07
  • I'm struggling to see why you are implementing a service which authenticates user access to a publicly available resource. – symcbean Jan 27 '15 at 13:11
  • okay lets say its not a username its a phrase to make it easy for a user to redirect to a subsite unique to that phrase for example you type apple it takes you to apple you type banana it takes you to banana etc etc – albaslayer Jan 27 '15 at 14:13

3 Answers3

0
if(isset($_SESSION['username'])){
    header("location: http://yoururl/" .$_SESSION['username']);
}
  • thats fine if i want to redirect to the main site i want to redirect to a link inside the database associated with the username – albaslayer Jan 27 '15 at 12:41
-1
 <?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 } ?>  

this should work out...

kheengz
  • 945
  • 10
  • 9
-1

Replace this with yours.

$result = mysql_query("SELECT redirect FROM members where username = '.$myusername.'");
adarsh hota
  • 315
  • 11
  • 23
  • 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 – albaslayer Jan 27 '15 at 12:52