[英]How to redirecting page if user directly entered page name in URL
如果用戶在URL中輸入頁面名稱,則必須重定向頁面。 我有兩個頁面,分別稱為index.php和shop.php
在index.php頁面中,有一個名為<a href="shop.php?function=add">Click here</a>
的鏈接。 如果我單擊鏈接,則頁面正確重定向到shop.php。
現在的問題是,如果有任何用戶直接在URL上輸入shop.php,則該頁面應在index.php頁面上重定向。 這意味着用戶將無法直接訪問shop.php頁面。
我不是在談論header('location')
我認為,您應該嘗試類似的方法:
if(isset($_GET['function']) && $_GET['function'] == 'add') {
// Show page contents
}
else {
header('location: index.php');
}
您應該嘗試在會話中添加隨機字符串
<?php
session_start();
$random = bin2hex(random_bytes(32));
$_SESSION['random'] = $random;
echo '<a href="shop.php?function=add&token='.$random.'">Click Here</a>';
?>
然后通過將其添加到shop.php中進行檢查
if(!isset($_GET['token']) || $_GET['token'] != $_SESSION['random']){
header('Location : index.php');
}
這樣,用戶將不可能直接訪問頁面
if(isset($_GET['function'])) {
if($_GET['function'] == 'add'){
// Show page if function is "add"
}
else {
// "Function" is not "add", it can be anything else: "remove", "edit", etc.
}
}
else {
header('location: index.php');
exit();
}
如果您不想使用header('location: index.php');
您可以使用。
?>
<script type="text/javascript">
window.location.href = 'index.php';
</script>
<?php
要么
$URL = 'index.php';
echo '<script type="text/javascript">document.location.href="' . $URL . '";</script>';
echo '<meta http-equiv="refresh" content="0; url=' . $URL . '">';
最后,我找到了解決方案。 我不知道我的代碼格式是否正確,但是我的問題已解決。
我從這里改變鏈接
<a href="shop.php?function=add">Click here</a>
to
<a href="shop.php?function=add&p_id=1">Click here</a>
並在shop.php中添加以下代碼
if ( !($_GET['function']='add' && $_GET['p_id'])){
header("Location: index.php");// send to login page
exit;
}
如果有什么問題請給我建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.