Имя пользователя для перенаправления URL

У меня установлена ​​базовая система с таблицей, которая должна считывать имя пользователя и URL, но я полный нуб, когда дело доходит до PHP, это то, что я получил, если кто-то может мне помочь, чтобы при вводе имени пользователя он автоматически перенаправлял их URL, связанный с этим аккаунтом, спасибо.

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

2

Решение

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

это должно сработать …

0

Другие решения

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

Замени это своим.

$result = mysql_query("SELECT redirect FROM members where username = '.$myusername.'");
0
По вопросам рекламы [email protected]