簡體   English   中英

PHP中重定向頁面中的授權控制

[英]Authorization control in redirected page in php

我有一個簡單的控制器,其中顯示要批准的確認。當用戶按下注冊按鈕時,將顯示確認頁面。

但是,如果用戶在未注冊的情況下將URL輸入為.... / confirmation,則會顯示該頁面。 我不希望不注冊就顯示它。

在asp.net mvc4中,可以使用ChildActionOnly注釋來完成。

可能嗎?

首先確保您已開始會話:

<?php session_start(); ?>

好的,這似乎很簡單-在注冊之后,以及在將用戶重定向到確認頁面之前,請執行以下操作(這自然是偽代碼 )。 假設$ user-> registered()作為注冊結果返回TRUE / FALSE,而$ user-> hasConfirmedRegistration()作為注冊確認結果返回TRUE / FALSE。 因此,您應該執行以下操作:

//this should be in your registration controller/function, i.e. /users/register
if ($user->registered()) {
   $_SESSION['showConfirmation'] = TRUE;
}

然后,應將其放在功能的開頭,以防止向未注冊用戶顯示確認頁面。

//This should be in your confirmation controller/function, 
//i.e. /users/confirm_registration:

//if user has not registered, do not show the page
if (! $_SESSION['showConfirmation']) {
   header('Location: /'); // redirect to main page
   return;
}

// -- enter code that handles storing confirmation, handling $_GET/$_POST etc. --

//then unset session variable, which is no longer needed
if ($user->hasConfirmedRegistration()) {
   unset($_SESSION['showConfirmation']);
}

我沒有完全看到您的代碼就無法完全理解您要實現的目標。 但這聽起來好像您不希望某人無需先執行操作即可訪問特定頁面。

這樣的事情可能會幫助您。

<?php
session_start();
if(!session_is_registered(somesessionamehere)){
header("location:form.php");
}
?>

當用戶提交表單時注冊會話,然后當他們轉到該頁面時,它將檢查會話是否已注冊。 如果不是,則重定向到上一頁。

查看此URL,以獲取基於登錄的示例: http : //www.phpeasystep.com/phptu/6.html

據我了解,您需要在確認頁面上檢查用戶是否剛剛發送了注冊數據。

例如,如果您的表單中有一個名為“ login”的輸入字段,則可以根據表單的“ method”屬性來檢查$_REQUEST$_POST$_GET中“ login”的存在和值。 如果不存在,則該表單尚未發布,您可以假定用戶只是輸入URL。 您可以將他重定向到登錄頁面。

<form method="post" action="/confirmation">
    <input type="text" name="login" />
    [...]
    <input type="submit" />
</form>
<?php
if (!isset($_POST["login"])) {
    // redirect
    header("HTTP/1.0 302 Found");
    header("Location: /login");

    return;
}

// show confirmation
// [...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM