問題描述
我在 PHP 工作.我想在登錄后將頁面重定向到我想訪問的最后一個頁面,但 5 小時后我仍然在這里堆棧,但我仍然沒有成功.這是架構(gòu),我有 3 個 php 文件.
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).
我正在使用 cookie 進行此重定向.首先我去了newest.php,然后我點擊了按鈕(去thread.php).然后thread.php看到你還沒有登錄,然后重定向到signin.php.在我填寫登錄表單后,我點擊了提交按鈕(signin.php),然后我在 signin.php 堆棧(不去任何地方)即使我已經(jīng)登錄,它應(yīng)該去 thread.php自動.
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.
這是我在 newest.php 中的代碼 &thread.php(不在 signin.php 中):
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,'/');
newest.php 中的提交按鈕(它轉(zhuǎn)到 thread.php):
submit button in newest.php (it goes to thread.php):
echo "<center><button onclick="window.location='/thread/form'">add new thread</button></center>";
在 signin.php 中(在我點擊提交按鈕或在提交區(qū)域后,因為表單和提交后我在同一頁面中)(在頁面底部):
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);
}
注意:在 signin.php 中,我在這個 cookie 之前還有另一個 cookie 設(shè)置,這是造成這個的原因嗎?或者我在一頁中設(shè)置了 2 個 cookie 有關(guān)系嗎?另一個cookie設(shè)置是這樣的(在頁面頂部)
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
推薦答案
我根本不會使用 cookie.
I would not use cookies at all.
方法一
一種可能的方法是將訪問的鏈接存儲到會話變量中,然后當用戶到達 login.php 頁面時,提供一個重定向到會話變量給定的 $url 的標頭.
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.
將此代碼粘貼到您網(wǎng)站或主容器上的所有頁面中.
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");
方法二
到目前為止,一個更簡單的解決方案就是擁有:
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']
只會保存當前頁面.你想要做的是使用 $_SERVER['HTTP_REFERER']
.因此,將 HTTP_REFERER 保存在表單的隱藏元素中,但還要注意,在處理表單的 PHP 中,如果登錄失敗,您將需要一些邏輯來重定向回登錄頁面,但還要檢查引用者是否實際上是您的網(wǎng)站,如果不是,則重定向回主頁.
$_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.
方法三
另一種常見的方法是通過 $_GET 變量將用戶的當前頁面?zhèn)鬟f到登錄表單.
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:
注意:$_SERVER['REQUEST_URI'] 是您當前的頁面
Note: $_SERVER['REQUEST_URI'] is your current page
header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
現(xiàn)在檢查它是否已填充,然后將用戶發(fā)送到:登錄.php
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" />
登錄檢查.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();
}
這篇關(guān)于在重定向系統(tǒng)中混淆此 cookie的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!