簡體   English   中英

將用戶重定向到特定頁面取決於輸入表單中給出的文本

[英]redirect user to specific pages depend on the text given on input form

我較新的 php 嘗試將用戶重定向到特定頁面,具體取決於輸入表單中給出的文本

我有一個帶按鈕的輸入和 php

 <input type="text" name="code" /> <input type="submit" value="Submit" />

 $pages = array( '123-admin-com' => 'alledite.php', '211-moderator-com' =>'halfedite.php', '056-coroot-com' => 'edite.php' ); if(isset($pages[$_POST['code']])): $page = $pages[$_POST['code']]; endif; header('Location: '. $page);

這項工作僅將用戶重定向到alledite.php如果用戶將全文123-admin-com我想要的是讓用戶重定向到alledite.php即使用戶在-admin-com之前放置任何東西都是隨機的例如用戶在之前放置隨機文本-admin-com喜歡

jhyjhgh-admin-comg0g646dfg-admin-com-admin-com之前的任何隨機文本比重定向到alledite.php所以我需要一些東西來驗證-admin-com

我想在其他211-moderator-com056-coroot-com上使用同樣的東西

希望你們理解我提前謝謝

根據描述,您需要檢查輸入是否以標識符之一結尾:

$pages = array(
    '-admin-com' => 'alledite.php',
    '-moderator-com' =>'halfedite.php',
    '-coroot-com' => 'edite.php'
);

if (isset($_POST['code'])) {
    $code = $_POST['code'];
    foreach ($pages as $id => $page) {
        if (endsWith($code, $id) { // if PHP 8.0 str_ends_with can be used 
            header('Location: ' . $page);
            //exit or return or ...
        }
    }
    //handle the case when $code is not valid
}
//handle the case when $_POST['code'] is not set

function endsWith ($string, $needle) {
    return substr($string, -$length) === $needle;
}
    

將您的 html 代碼放在 FORM 標簽中,就像:

<form action='redirect.php' method='POST'>
    <input type="text" name="code" />
    <input type="submit" value="Submit" />
</form>

此外,將以下代碼放在您的 IF 案例之前。

$page = "default_page.php"; //in case user providing a value does not exists in array, redirect to this page
if(isset($pages[$_POST['code']])):
    $page = $pages[$_POST['code']];
endif;

另一種方法可能是使用regex ——我知道還有其他方法遠比我更擅長編寫殺手級正則表達式模式。

<?php
    # for test porpoises
    define('BR','<br />');
    
    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['code'] ) ){
        # set $displayonly=false to actually do the redirection
        $displayonly=true;
        
        
        $code=filter_input(INPUT_POST,'code',FILTER_SANITIZE_STRING);
        $code=str_replace('-','.',$code);
        
        $pages=array(
            '@admin-com'            =>  'alledite.php',
            '@moderator-com'        =>  'halfedite.php',
            '@coroot-com'           =>  'edite.php'
        );
        
        foreach( $pages as $str => $redirect ){
            # construct a basic regex pattern from the array key.
            $pttn=sprintf( '/((.*)?%s)/', preg_quote( str_replace( '-', '.', $str ) ) );
            
            # for testing
            if( $displayonly )echo $pttn . BR;
            
            # Run the regex and if a match is found use the corresponding
            # value from the above array as the redirection endpoint.
            preg_match( $pttn, $code, $matches );
            if( !empty( $matches ) ){
                $header=sprintf('Location: %s', $redirect );
                exit( $displayonly ? $header : header( $header ) );
            }
        }
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>Match string and redirect</title>
    </head>
    <body>
        <form method='post'>
            <input type='text' name='code' />
            <input type='submit' />
        </form>
    </body>
</html>

由於它似乎是您希望匹配以執行重定向的任何字符串的后一部分,因此修改了到端點的字符串數組以刪除不必要的字符串。

暫無
暫無

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

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