代码之家  ›  专栏  ›  技术社区  ›  Hayden

有没有一种更短、更优化的方法来检查get参数?

php
  •  0
  • Hayden  · 技术社区  · 6 年前

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

    抱歉,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>
    2 回复  |  直到 6 年前
        1
  •  2
  •   Phil    6 年前

    我要结合 null-coalescing operator 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;
    

    从PHP7.0开始提供。

        2
  •  1
  •   dognose    6 年前

    至少你可以救自己一些 筑巢 :

    使用时 and ,如果最左边的子表达式已经为false,则任何表达式都将为false。右边的任何表达式都不会被求值。

    因此,您可以使用:

    if(isset($_GET['id']) && is_numeric($_GET['id'])){
       //show content
    }else{
       include '../partials/sorry.php';
    }
    

    is_numeric($_GET['id']) 不会被评估,如果 isset($_GET['id']) 返回false,因此可能的空引用没有问题。