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
Advertisement
Answer
<?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…