javascript 登录并重定向到用户主页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10027875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login and redirect to user home page
提问by Dhruvisha
The problem I can't currently solve is when a user logs in. They arrive at the site (index.php) and enter username and password, which gets submitted via a Post form back to index.php - if there are incorrect details then they get an error message. But if successful then I would like them to be taken to their user home page - but I can't do this! I am left presenting them with a link to the home page, which is more than a little clunky.
我目前无法解决的问题是当用户登录时。他们到达站点 (index.php) 并输入用户名和密码,然后通过 Post 表单将其提交回 index.php - 如果详细信息不正确,则他们收到一条错误消息。但是如果成功,那么我希望他们被带到他们的用户主页 - 但我不能这样做!我只剩下向他们展示一个指向主页的链接,这有点笨拙。
Seems there must be an obviously solution - never seen a site before that didn't redirect! Not sure is the answer is PHp, HTML, or Javascript.
似乎必须有一个明显的解决方案 - 以前从未见过没有重定向的网站!不确定答案是 PHp、HTML 还是 Javascript。
EDIT:
编辑:
Sorry if the question wasn't up to scratch - still learning here. Have tried to put the redirect in an if statement but still won't work. I think ideally the redirect would be top of the code, but can't see how to do that and retain the login procedure? Code as follows:
对不起,如果问题没有达到标准 - 仍然在这里学习。试图将重定向放在 if 语句中,但仍然无效。我认为理想情况下重定向将是代码的顶部,但不知道如何做到这一点并保留登录过程?代码如下:
if (isset($_POST['user']))
{
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
$salt = "randomsalt";
$md5pass = md5($salt . $pass);
if ($user == "" || $pass == "")
{
$error = "Not all fields were entered<br />";
}
else
{
$query = "SELECT username,password FROM users
WHERE username='$user' AND password='$md5pass'";
if (mysql_num_rows(queryMysql($query)) == 0)
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
header("Location: http://localhost:8888/profile.php?view=$user");
exit;
}
}
}
}
Thanks
谢谢
Rich
富有的
EDIT 2:
编辑2:
Thanks for your help guys - still couldn't resolve then need to have the re-direct at the top of the page, but equally having to have my login script in place. In the end solved it with:
感谢您的帮助 - 仍然无法解决然后需要在页面顶部重新定向,但同样必须有我的登录脚本。最后解决了它:
echo "<meta http-equiv=\"refresh\" content=\"0;URL=profile.php?view=$user\">";
In place of the header and exit rows above.
代替上面的标题和退出行。
Thanks again
再次感谢
Rich
富有的
回答by CFL_Jeff
You are probably redirecting wrong. First, put this at the very top of index.php
:
您可能重定向错误。首先,把它放在最顶部index.php
:
<?php
/* Redirect browser */
header("Location: http://newPath/newPage.php/");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
This is a simple redirect with an exit
statement to stop all processing afterwards, so you can just test to see if the redirect is working. If it works, then add a condition to the redirect, so something like
这是一个简单的重定向,带有一条exit
语句,用于在之后停止所有处理,因此您可以测试重定向是否有效。如果有效,则向重定向添加条件,例如
<?php
if (/*login credentials are valid*/){
/* Redirect browser */
header("Location: http://newPath/newPage.php/");
/* Make sure that code below does not get executed when we redirect. */
exit;
}
?>
but, of course replace /*login credentials are valid*/
with a call to a function or whatever that actually checks the credentials and returns a boolean value.
但是,当然替换/*login credentials are valid*/
为对函数的调用或任何实际检查凭据并返回布尔值的内容。
回答by m-t
this solution is for php, hope this will help you.
此解决方案适用于 php,希望对您有所帮助。
$user_name = $_POST['username']; // posting value through form
$pass_word = $_POST['password']; // posting value through form
$sql = mysql_query("select 1 from admin_users where username='".$user_name."' and password='".$pass_word."'"); // checking username and password in database
if ($sql)
{echo "Invalid User Name/Password";}
else
{header("Location: homepage.php"); exit;}
回答by Dhruvisha
take username and password in a variable.
在变量中输入用户名和密码。
$username = $_POST['username'];
$pwd = $_POST['password'];
Compare this with your table like below:
将此与您的表格进行比较,如下所示:
$sql = mysql_query("select * from tbl_users where username='".$username."' and password='".$pwd."'");
if(mysql_num_rows($sql)>0){
header("Location:homepage.php");
}else{
echo "Invalid username or password ";
}
This will work.
这将起作用。