混淆关于这个cookie在重定向系统 [英] confusing about this cookies in redirecting system

查看:198
本文介绍了混淆关于这个cookie在重定向系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP工作。我想重定向页面登录后,我想访问的最后一页,但我仍然在这里在5个小时堆叠,我仍然没有使它。这是模式,我有3个php文件。

  newest.php(登录前),
signin.php(登录前),
线程。 php(登录后)。

我正在使用Cookie进行此重定向。首先我去了newest.php,然后我点击按钮(去thread.php)。然后thread.php看到你还没有登录,然后重定向到signin.php。然后,我填写登录表单,然后,我点击提交按钮(signin.php),然后我堆栈在signin.php(不去任何地方,即使我已经登录,它应该去thread.php自动。



这是我的代码在newest.php& thread.php(not in signin.php):

  $ coopage ='coopage' 
$ current_page ='http://'.$_SERVER [HTTP_HOST]。$ _ SERVER [REQUEST_URI];
setcookie($ coopage,$ current_page,time()+ 86400,'/');

在newest.php中提交按钮(转到thread.php):

  echo< center>< button onclick = \window.location ='/ thread / form'\> add new thread< ; / button>< / center>; 

在我点击提交按钮或提交区域后,因为表单和提交后我在同一页面)(在页面底部):

  if(isset($ _ COOKIE [$ coopage] )){
$ url = $ _ COOKIE [$ coopage];
unset($ _ COOKIE [$ coopage]);
header('location:'。$ url);
}

注意:在signin.php中,是这个原因吗?或者如果我有一个页面中有2个cookie设置?另一个Cookie设置如下(在页面顶部)

  $ cooval2 ='juna' 
setcookie($ coousername,$ cooval2,time()+(3600 * 24 * 365),/); // 1年


解决方案



一种可能的方式可以是存储被访问的链接到会话变量,然后当用户到达login.php页面时,提供一个头部重定向到由会话变量给定的$ url。



将此代码粘贴到所有您的网站或主容器上的网页。

 <?php 
session_start
$ _SESSION ['url'] = $ _SERVER ['REQUEST_URI'];

对于登录页面,您可以:

 <?php 
session_start(); //需要会话。
if(isset($ _ SESSION ['url']))
$ url = $ _SESSION ['url']; //保存最后访问的网址。
else
$ url =student_account.php;

header(Location:http://example.com/$url);

方法2



到目前为止,更简单的解决方案是:

 < hidden name =redirurlvalue =< ?echo $ _SERVER ['HTTP_REFERER'];?> /> 

然后在他们登录后重定向到该地址。


$ b $



$ _ SERVER ['REQUEST_URI']

code>将简单地保存当前页面。你想做的是使用 $ _ SERVER ['HTTP_REFERER']
因此,将HTTP_REFERER保存在表单上的隐藏元素中,但还要注意在处理表单的PHP中,如果登录失败,您将需要一些重定向回登录页面的逻辑,

方法3





$ p
$ b

另一种常见的方法是通过$ _GET变量将用户当前页面传递给登录表单。



更改脚本

注意: $ _ SERVER ['REQUEST_URI'] 是您当前的网页 p>

  header(Location:login.php?location =。urlencode($ _ SERVER ['REQUEST_URI'])); 

现在检查它是否已填充,然后将用户发送到:
login.php

  echo'< input type =hiddenname =locationvalue ='; 
if isset($ _ GET ['location'])){
echo htmlspecialchars($ _ GET ['location']);
}
echo'/>';
//将显示如下:
//< input type =hiddenname =locationvalue =previousPage.php/>

login-check.php

  session_start(); 

//我们的url现在存储为$ _POST ['location'](从login.php发布)。如果它是空白的,让我们忽略它。否则,让我们做一些事情。
$ redirect = NULL;
if($ _ POST ['location']!=''){
$ redirect = $ _POST ['location'];
}

if((empty($ username)OR empty($ password)AND!isset($ _ SESSION ['id_login'])){
$ url =' login.php?p = 1';
//如果我们有一个重定向URL,将它传回到login.php,所以我们不会忘记它
if(isset($ redirect)){
$ url。='& ; location ='。 urlen code($ redirect);
}
header(Location:。$ url);
exit();
}
elseif(!user_exists($ username,$ password)AND!isset($ _ SESSION ['id_login'])){
$ url ='login.php?p = ;
if(isset($ redirect)){
$ url。='& location ='。 urlencode($ redirect);
}
header(Location:。$ url);
exit();
}
elseif(isset($ _ SESSION ['id_login'])){
//如果登录成功并且有重定向地址,直接向用户发送
if ($ redirect)){
header(Location:。$ redirect);
} else {
header(Location:login.php?p = 3);
}
exit();
}


I work in PHP. I want to redirect page after login to the last page that i want to visit, but I'm still stack at here in 5 hours and I still don't make it yet. This is the schema, I have 3 php file.

newest.php (before login), 
signin.php (before login), 
thread.php (after login). 

I'm using cookies for this redirecting. First i went to the newest.php, then i clicked the button (go to thread.php). Then thread.php saw that you haven't loggin yet, then redirected to signin.php. After i fill the signin form then, i clicked the submit button (the signin.php), then I'm stack at signin.php (not going anywhere) even after I've loggin in, it should be go to thread.php automatically.

this is my code in newest.php & thread.php (not in signin.php):

$coopage='coopage';
$current_page='http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
setcookie($coopage, $current_page,time()+86400,'/');

submit button in newest.php (it goes to thread.php):

echo "<center><button onclick=\"window.location='/thread/form'\">add new thread</button></center>"; 

in signin.php (after i clicked the submit button or in submit area, because form and after submit i made in the same page) (in the bottom of the page):

if(isset($_COOKIE[$coopage])){
    $url=$_COOKIE[$coopage];
    unset($_COOKIE[$coopage]);
    header('location:'.$url);
}

note: in signin.php i also have another cookie setup before this cookie, is that the cause of this? or does it matter if i have 2 cookies setup in one page? Another cookie setup is like this (at the top of the page)

$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year

解决方案

I would not use cookies at all.

Method 1

A possible way could be to store the link visited into a session variable and then when the user reaches the login.php page, provide a header redirect to $url given by the session variable.

Paste this code into all your pages on your website or the main container.

<?php
session_start(); 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 

For the login page you can have:

<?php
session_start();  // needed for sessions.
if(isset($_SESSION['url'])) 
   $url = $_SESSION['url']; // holds url for last page visited.
else 
   $url = "student_account.php"; 

header("Location: http://example.com/$url"); 

Method 2

A simpler solution by far is simply to have:

<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />

Then redirect to that address once they log in.

However, this is only good if you have a login box on every page.

$_SERVER['REQUEST_URI'] will simply hold the current page. What you want to do is use $_SERVER['HTTP_REFERER']. So save the HTTP_REFERER in a hidden element on your form, but also take note on that in the PHP that processes the form you will need some logic that redirects back to the login page if login fails but also to check that the referer is actually your website, if it isn't, then redirect back to the homepage.

Method 3

Another common way to do this is to pass the user's current page to the Login form via a $_GET variable.

change your script so that is also tells the login page to remember where you are:

Note: $_SERVER['REQUEST_URI'] is your current page

header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));

Now check if it is populated, then send the user to this: login.php

echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
    echo htmlspecialchars($_GET['location']);
}
echo '" />';
//  Will show something like this:
//  <input type="hidden" name="location" value="previousPage.php" />

login-check.php

session_start();

//  our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
    $redirect = $_POST['location'];
}

if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
    $url = 'login.php?p=1';
    // if we have a redirect URL, pass it back to login.php so we don't forget it
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location: " . $url);
   exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
    $url = 'login.php?p=2';
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location:" . $url);
   exit();
}
elseif(isset($_SESSION['id_login'])) {
    // if login is successful and there is a redirect address, send the user directly there
    if($redirect)) {
        header("Location:". $redirect);
    } else {
        header("Location:login.php?p=3");
    }
    exit();
}

这篇关于混淆关于这个cookie在重定向系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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