php - Login and redirect to user home page - Stack Overflow

The problem I can't currently solve is when a user logs in. They arrive at the site (index.php) an

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
Add a ment  | 

3 Answers 3

Reset to default 4

You 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

相关推荐

  • php - Login and redirect to user home page - Stack Overflow

    The problem I can't currently solve is when a user logs in. They arrive at the site (index.php) an

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信