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.
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.
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 (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:
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
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.
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.
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 (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:
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
Share Improve this question edited Apr 10, 2012 at 18:38 Rich 1173 bronze badges asked Apr 5, 2012 at 9:50 RichRich 1- 2 "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" - Not sure how we are meant to help, there really isn't enough information here to help. – ozz Commented Apr 5, 2012 at 9:59
3 Answers
Reset to default 4You are probably redirecting wrong. First, put this at the very top of 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
<?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.
this solution is for php, hope this will help you.
$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;}
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580221a4634209.html
评论列表(0条)