在处理程序之后将用户重定向回index.php [英] To redirect an user back to index.php after a handler

查看:206
本文介绍了在处理程序之后将用户重定向回index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把用户名和密码放到我的一种形式。该操作启动一个handler.php。
如果用户没有在handler.php重新加载浏览器,那么用户只能看到一个白页(handler.page)。



我想让用户在处理handler.php后自动回到主页。获得登录-cookie。



 code> $ email = $ _POST ['email']; 
$ username = $ _POST ['username'];
$ passhash_md5 = $ _POST ['passhash_md5'];

// COOKIE设置

/ * $ cookie可能看起来像这样
变量
$ username =username$
$ passhash_md5 =password$
$ email =email$
$ _SERVER ['REMOTE_ADDR'] = 11.44.23.94 $
before md5:$
usernamepasshash_md5email11.44.23.94 $
after md5:$
a08d367f31feb0eb6fb51123b4cd3cb7$
* /

$ login_cookie = md5(
$ username。
$ password 。
$ email。
$ _SERVER ['REMOTE_ADDR']
);

setcookie(login,$ login_cookie);

if(isset($ _ COOKIE ['login']))
{

$ sql2 =SELECT * from users
$ raw_user_list = pg_query($ dbconn,$ sql2);
$ user_list = pg_fetch_all($ raw_user_list);

//处理用户列表中具有密码的每个用户
foreach($ user_list为$ user => $ passhash_md5)
{
/ /匹配用户列表和cookie $
if($ login_cookie == $ _COOKIE ['login'])
{
header(Location:index.php);
die(logged in);
}
}
header(Location:index.php);
die(wrong username / password);
}
?>

我有一个使用POST方法的表单,操作是handler.php。



我的表单

 < form method = postaction =handler.php> 
< p>用户名:
< input name =usernametype =textsize =40/>
< / p>

< p>电子邮件:
< input name =emailtype =textsize =230/>
< / p>

< p>密码:
< input name =passwordtype =passwordsize =230/&
< / p>

< input type =submitvalue =OK/>
< / form>

AJAX未调用处理程序页面。



我用HEAD运行处理程序页面失败:

 < head> 
< meta http-equiv =refreshcontent =5; URL = inedx.php>
< / head>

但是,我不能包括HEAD,因为PHP不允许在使用header命令时输出



如果登录成功,如何将用户自动添加到index.php中?



首先,用户进入一个登录页面,并将他们的用户名密码。我们称之为login.php。然后将资料传送给handler.php



HTML

  ; form method =POSTaction =handler.php> 
< input type =textname =login [user]>
< input type =passwordname =login [password]>
< / form>

然后,处理程序脚本接收POST数据,处理if,如果密码哈希匹配,设置

  //检查登录表单
if(isset($ _ POST ['login']))
{
//获取数据
$ sql2 =SELECT * from用户;
$ raw_user_list = pg_query($ dbconn,$ sql2);
$ user_list = pg_fetch_all($ raw_user_list);

//遍历每个用户
foreach($ user_list as $ user => $ passhash_md5)
{
//检查密码是否匹配
if($ passhash_md5 == md5($ _ POST ['login'] ['password']))
{
//你需要创建一个COOKIE

(Location:index.php);
die(logged in);
}
}
header(Location:index.php);
die(wrong username / password);
}

然后,在每个要检查登录的页面上,如果他们没有设置登录cookie。

 <$ c 

$ c> //检查Cookie
if(!$ _ COOKIE ['login'])
{
header('Location:login.php');
die(User Required);
}



我不太确定你在那里做什么,是如何创建基本登录表单的基本设置。






如果您尝试检查组合是否通过

  //设置变量
$ email = $ _POST ['电子邮件'];
$ username = $ _POST ['username'];
$ passhash_md5 = $ _POST ['passhash_md5'];

// COOKIE设置

/ * $ cookie可能看起来像这样
变量
$ username =username$
$ passhash_md5 =password$
$ email =email$
$ _SERVER ['REMOTE_ADDR'] = 11.44.23.94 $
before md5:$
usernamepasshash_md5email11.44.23.94 $
after md5:$
a08d367f31feb0eb6fb51123b4cd3cb7$
* /

//设置cookie应该是什么样子
$ login_cookie = md5
$ username。
$ password。
$ email。
$ _SERVER ['REMOTE_ADDR']
);

//检查Cookie
if(isset($ _ COOKIE ['login']))
{
//检查登录表单是否相同作为cookie
if($ login_cookie == $ _COOKIE ['login'])
{
header(Location:index.php);
die(logged in);
}
header(Location:index.php);
die(wrong username / password);
}



我取出了数据库部分,因为你没有使用数据库部分任何代码,所以没关系。看起来你不是试图记录某人,而是检查他们设置到他们的机器的cookie包含他们在表单上传递的相同的字符串。



< hr>

好的,最后的版本,希望

  //设置变量
$ email = $ _POST ['email'];
$ username = $ _POST ['username'];
$ password = $ _POST ['password'];

// COOKIE设置

/ * $ cookie可能看起来像这样
变量
$ username =username$
$ passhash_md5 =password$
$ email =email$
$ _SERVER ['REMOTE_ADDR'] = 11.44.23.94 $
before md5:$
usernamepasshash_md5email11.44.23.94 $
after md5:$
a08d367f31feb0eb6fb51123b4cd3cb7$
* /

//设置cookie应该是什么样子
$ login_cookie = md5
$ username。
$ password。
$ email。
$ _SERVER ['REMOTE_ADDR']
);

//检查Cookie
if(isset($ _ COOKIE ['login']))
{
//检查登录表单是否相同作为cookie
if($ login_cookie == $ _COOKIE ['login'])
{
header(Location:index.php);
die(logged in);
}
header(Location:index.php);
die(wrong username / password);
}
//如果没有cookie,尝试将它们记录在
中。
{
$ sql2 = sprintf(SELECT * from users WHERE passhash_md5 ='%s' ,
pg_escape_string($ login_cookie));
$ raw_user_list = pg_query($ dbconn,$ sql2);
if($ user = pg_fetch_row($ raw_user_list)){。
setcookie ('login',$ login_cookie);
header(Location:index.php);
die(logged in);
} else {
header位置:index.php);
die(wrong username / password);
}
}

Rezzif 提供的Sprintf和Where子句


I put "username" and "password" to a form of mine. The action starts up a handler.php. The user sees then only a white page (handler.page) if he does not reload his browser at handler.php. If he does, the handler puts him to back to index.php.

I would like to put the user automatically back to the homepage after being at handler.php where he gets a login -cookie.

I have the following in my handler.php

$email = $_POST['email'];
$username = $_POST['username'];
$passhash_md5 = $_POST['passhash_md5']; 

 // COOKIE setting

 /* $cookie may look like this
   variables
        $username = "username"$
        $passhash_md5 = "password"$
        $email ="email"$
        $_SERVER['REMOTE_ADDR']=11.44.23.94$
   before md5:$
        "usernamepasshash_md5email11.44.23.94"$
   after md5:$
        "a08d367f31feb0eb6fb51123b4cd3cb7"$
 */

$login_cookie = md5(                                                                                                                                                                           
    $username .
    $password .
    $email .
    $_SERVER['REMOTE_ADDR']
);

setcookie ("login", $login_cookie);    

if (isset($_COOKIE['login']) )
{

    $sql2 = "SELECT * from users";
    $raw_user_list = pg_query($dbconn, $sql2);
    $user_list = pg_fetch_all($raw_user_list);

    // to process each user in the user-list that has a password 
    foreach ($user_list as $user => $passhash_md5)
    {                                                                                                                                                                                               
        //match the user list with the cookie$
        if ( $login_cookie == $_COOKIE['login'] )
        {
            header("Location: index.php"); 
            die("logged in");
        }
    }
    header("Location: index.php");   
    die("wrong username/password");
}
?>      

I have a form which uses the POST -method and the action is handler.php.

My form

<form method="post" action="handler.php">
    <p>Username:
        <input name="username" type="text" size="40" />
    </p>

    <p>Email:
        <input name="email" type="text" size="230" />
    </p>

    <p>Password:
        <input name="password" type="password" size="230" />
    </p> 

    <input type="submit" value="OK" />
</form>

The handler page is not being called by AJAX.

I run the handler page unsuccessfully with the HEAD:

<head>
<meta http-equiv="refresh" content="5; URL=inedx.php">
</head>

However, I cannot include the HEAD because PHP does not allow to have output when you use header -commands.

How can you put the user automatically to the index.php if the login is successful?

解决方案

This should be your basic setup

First, the user comes to a login page and puts in their username/password. We'll call this login.php. It then sends the stuff to handler.php

HTML

<form method="POST" action="handler.php">
<input type="text" name="login[user]">
<input type="password" name="login[password]">
</form>

Then, the handler script recieves the POST data, processes if, and if the password hashes match, set a cookie and redirect back to the index page.

Login Script

// Check for a Login Form
if (isset($_POST['login']) )
{
    // Get the Data
    $sql2 = "SELECT * from users";
    $raw_user_list = pg_query($dbconn, $sql2);
    $user_list = pg_fetch_all($raw_user_list);

    // Go through each User 
    foreach ($user_list as $user => $passhash_md5)
    {   
        // Check if the passwords match
        if ( $passhash_md5 == md5($_POST['login']['password'] ))
        {
            // YOU NEED TO CREATE A COOKIE HERE     

            header("Location: index.php"); 
            die("logged in");
        }
    }
    header("Location: index.php");   
    die("wrong username/password");
}

Then, on every page you want to check for login, you redirect someone away if they don't have a login cookie set. You could expand this to check for a correct login cookie.

Every Page

// Check for a Cookie
if(!$_COOKIE['login'])
{
    header('Location: login.php');
    die("User Required");
}

I'm not too certain what you were trying to do there, but this is the basic set up for how to create a basic login form.


If you are try to check if the combination passed into the form is the same as the cookie try this:

// Set the Variables
$email = $_POST['email'];
$username = $_POST['username'];
$passhash_md5 = $_POST['passhash_md5']; 

 // COOKIE setting

 /* $cookie may look like this
   variables
        $username = "username"$
        $passhash_md5 = "password"$
        $email ="email"$
        $_SERVER['REMOTE_ADDR']=11.44.23.94$
   before md5:$
        "usernamepasshash_md5email11.44.23.94"$
   after md5:$
        "a08d367f31feb0eb6fb51123b4cd3cb7"$
 */

// Set what the cookie should look like
$login_cookie = md5(                                                                                                                                                                           
    $username .
    $password .
    $email .
    $_SERVER['REMOTE_ADDR']
);

// Check For the Cookie
if (isset($_COOKIE['login']) )
{
    // Check if the Login Form is the same as the cookie
    if ( $login_cookie == $_COOKIE['login'] )
    {
        header("Location: index.php"); 
        die("logged in");
    }
    header("Location: index.php");   
    die("wrong username/password");
}

I took out the database part because you aren't using the database part in any of the code, so it doesn't matter. It looks like you aren't trying to log someone in, but rather check that the cookie they have set to their machine contains the same string that they passed in on the form.


Ok, final edition, hopefully

// Set the Variables
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password']; 

 // COOKIE setting

 /* $cookie may look like this
   variables
        $username = "username"$
        $passhash_md5 = "password"$
        $email ="email"$
        $_SERVER['REMOTE_ADDR']=11.44.23.94$
   before md5:$
        "usernamepasshash_md5email11.44.23.94"$
   after md5:$
        "a08d367f31feb0eb6fb51123b4cd3cb7"$
 */

// Set what the cookie should look like
$login_cookie = md5(                                                                                                                                                                           
    $username .
    $password .
    $email .
    $_SERVER['REMOTE_ADDR']
);

// Check For the Cookie
if (isset($_COOKIE['login']) )
{
    // Check if the Login Form is the same as the cookie
    if ( $login_cookie == $_COOKIE['login'] )
    {
        header("Location: index.php"); 
        die("logged in");
    }
    header("Location: index.php");   
    die("wrong username/password");
}
// If no cookie, try logging them in
else
{
    $sql2 = sprintf("SELECT * from users WHERE passhash_md5='%s',
    pg_escape_string($login_cookie));
    $raw_user_list = pg_query($dbconn, $sql2);
    if ($user = pg_fetch_row($raw_user_list)) {.
        setcookie('login', $login_cookie);
        header("Location: index.php"); 
        die("logged in");
    } else {
    header("Location: index.php");   
    die("wrong username/password");
    }
}

Sprintf and Where clause provided by Rezzif

这篇关于在处理程序之后将用户重定向回index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆