我创建了一个数据库(遵循在线教程),该数据库将所有用户定向到一个登录页面。我想知道如何将不同的用户引导到不同的网页。例如,我想将JOHN SMITH(user1)指向localhost / pages / johnsmith.html和JANE SMITH(用户2)到localhost / pages / janesmith.html。
代码:
db_const.php
<?php
# mysql db constants DB_HOST, DB_USER, DB_PASS, DB_NAME
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = 'root';
const DB_NAME = 'ClientDashboard';
?>
的login.php
<html>
<head>
<title>User Login Form - PHP MySQL Ligin System | W3Epic.com</title>
</head>
<body>
<h1>User Login Form - PHP MySQL Ligin System | W3Epic.com</h1>
<?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<?php
} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
echo "<p>Logged in successfully</p>";
// do stuffs
}
}
?>
</body>
</html>
register.php
<html>
<head>
<title>User registration form- PHP MySQL Ligin System | W3Epic.com</title>
</head>
<body>
<h1>User registration form- PHP MySQL Ligin System | W3Epic.com</h1>
<?php
require_once("db_const.php");
if (!isset($_POST['submit'])) {
?> <!-- The HTML registration form -->
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
First name: <input type="text" name="first_name" /><br />
Last name: <input type="text" name="last_name" /><br />
Email: <input type="type" name="email" /><br />
<input type="submit" name="submit" value="Register" />
</form>
<?php
} else {
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli- >connect_error}</p>";
exit();
}
## query database
# prepare data for insertion
$username = $_POST['username'];
$password = $_POST['password'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
# check if username and email exist else insert
$exists = 0;
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1");
if ($result->num_rows == 1) {
$exists = 1;
$result = $mysqli->query("SELECT email from users WHERE email = '{$email}' LIMIT 1");
if ($result->num_rows == 1) $exists = 2;
} else {
$result = $mysqli->query("SELECT email from users WHERE email = '{$email}' LIMIT 1");
if ($result->num_rows == 1) $exists = 3;
}
if ($exists == 1) echo "<p>Username already exists!</p>";
else if ($exists == 2) echo "<p>Username and Email already exists! </p>";
else if ($exists == 3) echo "<p>Email already exists!</p>";
else {
# insert data into mysql database
$sql = "INSERT INTO `users` (`id`, `username`, `password`, `first_name`, `last_name`, `email`)
VALUES (NULL, '{$username}', '{$password}', '{$first_name}', '{$last_name}', '{$email}')";
if ($mysqli->query($sql)) {
//echo "New Record has id ".$mysqli->insert_id;
echo "<p>Registred successfully!</p>";
} else {
echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error} </p>";
exit();
}
}
}
?>
</body>
</html>
谢谢!
答案 0 :(得分:2)
您的查询中存在一些安全问题,但只是为了回答您实际要求的部分,您可以添加标题(“Location:”。$ url)将重定向到任何页面。
//if (!$result->num_rows == 1) {
if($result->num_rows !==1){
echo "<p>Invalid username/password combination</p>";
} else {
echo "<p>Logged in successfully</p>";
// do stuffs
header('Location:/' . $result->firstname. $result->lastname. '.html');
}
答案 1 :(得分:0)
这里最常见的方法是拥有一些通用用户页面,让我们说user.php
,以$_GET
参数接收用户的id
。然后,当用户注册时,您将其重定向到user.php?id=1
,其中1将是注册用户的ID。
答案 2 :(得分:0)
使用开关语句可以使用标题
switch ($userName) {
case "JOHN SMITH":
header('location: localhost/pages/johnsmith.html');
break;
case "JANE SMITH":
header('location: localhost/pages/janesmith.html');
break;
default:
header('location: /noname.html');
}
如果您的用户很少,这是可以的,但是如果您有很多用户,则需要存储该用户的页面网址,然后在成功登录时重定向到该用户
if(USERLOGEDIN){
header('location: ' . $userHomePage);
}
答案 3 :(得分:0)
解决方案的类型取决于,如果你只有一些指定的用户,你只需要将一个if语句重定向到login.php中的指定页面,如:
if (username == 'Whatever the username might be') {
Header("Location: file.html");
} elseif (username == 'Some other user') {
Header("Location: file2.html");
} //so on
当然,这是在验证密码和用户名之后。
我希望这有帮助,如果你想要更大的东西,我不确定解决方案是什么。
(如果您收到错误提示标题已存在,请使用此标记:
ob_start();
在<?php
标记之上的<html>
之后,如果使用过多的标题函数,有时可能会出错,ob start应解决此问题。)
答案 4 :(得分:0)
在提交正确的用户/通行证组合后,它所指向的代码并不完全清楚。
看起来这是验证后做的任何事情的唯一部分:
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
echo "<p>Logged in successfully</p>";
// do stuffs
}
如果是这样,请尝试将其更改为以下代码。请注意,此解决方案是必需的JavaScript,因为您已经发送了标头,因此无法进行元刷新。
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
while ($row = $result->fetch_assoc()) {
echo "<p>Logged in successfully</p>";
// do stuffs
echo '<script type="text/javascript">';
echo 'window.location = "http://localhost/pages/.' .$row['first_name']. $row['last_name']. '.htm";';
echo '</script>';
}
}
另请注意,我的代码是通过从数据库中提取名字和名字来合并它们来查找页面名称。如果需要,您可以使用更加手动的方法,但这样您就不需要编辑此代码来添加其他用户。