首页 > 解决方案 > 是否有更短、更优化的方法来检查 GET 参数?

问题描述

我正在检查 URI 中的参数是否存在。然后,我正在检查它是否是一个数字,然后我正在检查论坛帖子是否存在。有没有更短、更好的方法来代替重复自己?

sorry.php 页面是我包含的错误页面。

我什至不知道我是否正确地处理了这件事。我对此还是很陌生。

<!DOCTYPE html>
<html lang="en">
    <head>
        <?php 
        //require head
        require '../partials/head.php';
        ?>
        <title>KTOWN | </title>
    </head>
    <body>
        <?php 
        //sidenav 
        include '../partials/sidenav.php';
        ?>
        <div class="container">
            <?php 
            //header 
            include '../partials/header.php';
            ?>
            <main>
                <?php 

                if(!isset($_GET['id'])){

                    include '../partials/sorry.php';

                }else{
                    if(!is_numeric($_GET['id'])){

                        include '../partials/sorry.php';

                    }else{
                        $postId = (int)$_GET['id'];
                        $qry = '
                        SELECT forum_posts.id AS postId, forum_cats.name AS catName, site_users.username AS postAuthor, forum_posts.post_title AS postTitle, forum_posts.post_content AS postContent, forum_posts.is_anon AS postAnon, forum_posts.show_post AS showPost
                        FROM forum_cats 
                        JOIN forum_posts ON forum_cats.id = forum_posts.post_cat_id
                        JOIN site_users ON site_users.id = forum_posts.post_author_id
                        WHERE forum_posts.id = ?';
                        $getPost = $conn->prepare($qry);
                        $getPost->bind_param('i',$postId);
                        $getPost->execute();
                        $result = $getPost->get_result();

                        if($result->num_rows < 1){
                            include '../partials/sorry.php';
                        }else{

                            while($row = $result->fetch_object()){
                                $postId = $row->postId;
                                $postTitle = $row->postTitle;
                                $postAuthor = $row->postAuthor;
                                $postAnon = $row->postAnon;
                                $showPost = $row->showPost;
                                $postContent = $row->postContent;
                            }

                            if($showPost === 0){
                                include '../partials/sorry.php';
                            }else{
                ?>

                            <div class="forum_post">
                                <h1><?php echo $postTitle; ?></h1>
                                <?php 
                                if($postAnon === 1){
                                    echo '<h2 class="forum_post__author">by <i class="fa fa-user-secret"></i> Anonymous</h2>';
                                }else{
                                    echo '<h2 class="forum_post__author"> by '.$postAuthor.'</h2>';
                                }
                                ?>
                                <p class="forum_post__content"><?php echo $postContent; ?></p>
                                <button class="btn btn--red"><i class="fa fa-flag"></i> Report</button>
                            </div>
                            <form class="forum-reply-form">
                                <h2>Reply Here</h2>
                                <div class="fgrp">
                                    <textarea name="replyContent" id="replyContent" cols="30" rows="6" class="input input__textarea"></textarea>
                                </div>
                                <div class="fgrp">
                                    <button id="subbut" class="btn btn--orange">Submit</button>
                                </div>
                                <div class="errbox"></div>
                            </form>
                            <div style="box-shadow:0 0 3rem #ccc; margin:1rem;padding:1rem;"><h2><i class="fa fa-reply"></i> Replies</h2></div>
                    

                <?php 
                            }
                        }
                    }
                    }
                ?>
            </main>
        </div>
    </body>
    <script src="../js/sidenav.js"></script>
    <script>
            document.querySelector('#subbut').addEventListener('click',function(evt){
                evt.preventDefault();
                var content = document.querySelector('#replyContent').value.trim();
                var errs = 0;
                var errbox = document.querySelector('.errbox');
                errbox.innerHTML = '';
                var errmsg = [];
                if(content.length < 100){
                    errs++;
                    errmsg.push('<p>Please add more content to your reply.</p>');
                }
                if(errs !== 0){
                    for(var i =0; i < errmsg.length; i++){
                        errbox.insertAdjacentHTML('beforeend',errmsg[i]);
                    }

                }else{
                    errbox.innerHTML = '';

                    //Submit reply

                }



            });
    </script>
</html>

标签: php

解决方案


我会结合使用空合并运算符filter_var()...

if ($postId = filter_var($_GET['id'] ?? null, FILTER_VALIDATE_INT)) {
    // all good, $postId is numeric
} else {
    include '../partials/sorry.php';
}

??isset()三元语句的现代且不那么冗长的等价物,例如

$id = isset($_GET['id']) ? $_GET['id'] : $someDefaultValue;

自 PHP 7.0 起可用。


推荐阅读