PHP登陸後跳轉到登陸前頁面實現思路及程式碼
阿新 • • 發佈:2018-12-19
我自己寫了一個方法 如下
$_SERVER['HTTP_REFERER']
可以檢視上個頁面傳遞過來的引數
<?php header('content-type:text/html;charset=utf-8'); include_once './lib/fun.php'; judgeVx(); if (checkLogin()) { msg(1, '您已登入', 'index.php'); } //表單進行了提交處理 $gourl = 'index.php'; if (empty($_POST['username']) && strstr($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])) { $gourl = str_replace(getProtocol() . $_SERVER['HTTP_HOST'], "", $_SERVER['HTTP_REFERER']); } if (!empty($_POST['username']) && !empty($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; $res = login($username, $password); if ($res == "登入成功") { // 如果傳遞過來的域名 包含我們的域名 那麼就替換掉 並且跳轉 header("Location:" . $_POST['gourl']); } else { msg(2, $res); } } ?>
在登入的表單上多提交一個引數
<input type="hidden" name="gourl" value="<?php echo $gourl ?>">
<button style="submit" class="login_btn">登 錄</button>
function getProtocol() { //主動判斷是否HTTPS if (is_https()) { return "https://"; } else { return "http://"; } }
function is_https() { if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { return true; } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { return true; } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { return true; } return false; }