PHP while循环对于URL Shortner脚本无法正常工作

PHP while循环对于URL Shortner脚本无法正常工作,php,url-shortener,Php,Url Shortener,我正试图使网址缩短网站,我的问题是,PHP虽然循环工作不正常。问题是,我允许用户一次输入五个链接以缩短URL。我在本地主机上测试它,发现了这个问题。如果我添加一个链接,那么它可以正常工作。但是如果我添加了多个全部五个链接,那么它就不能正常工作。我的密码是 <?php class Shorten{ public static function check_archived_link($link, $user_email){ include $_SERVER['D

我正试图使网址缩短网站,我的问题是,PHP虽然循环工作不正常。问题是,我允许用户一次输入五个链接以缩短URL。我在本地主机上测试它,发现了这个问题。如果我添加一个链接,那么它可以正常工作。但是如果我添加了多个全部五个链接,那么它就不能正常工作。我的密码是

<?php

class Shorten{

    public static function check_archived_link($link, $user_email){

        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        $check_archived_query = "SELECT * FROM `url_table` WHERE BINARY (`url` = '$link' AND `u_email` = '$user_email' AND `archived` != '0')";

        if($result = $db->query($check_archived_query)){
            if($result AND $db->affected_rows > 0){

                $update_archived_link = "UPDATE `url_table` SET `archived` = '0' WHERE BINARY (`url` = '$link' AND `u_email` = '$user_email')";

                if(($db->query($update_archived_link)) AND ($db->affected_rows > 0)){
                    throw new Exception('Your entered link is already in use with your account and it was deactivated by you. We\'ve activated your link.<br>');
                    return true;
                } else {
                    throw new Exception('Your entered link is already in use with your account and it was deactivated by you. We tried to activate the links but we got some error. Please try again.<br>');
                }

            }           
            else {
                self::check_link($link, $user_email);
            }
        }

        else {
            throw new Exception('We faced some unwanted errors. Please try once again.<br>');
        }

    }

    public static function check_link($link, $user_email){

        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        $check_query = "SELECT * FROM `url_table` WHERE BINARY (`url` = '$link' AND `u_email` = '$user_email')";

        if($result = $db->query($check_query)){
            if($result AND $db->affected_rows > 0){
                throw new Exception('Your entered link is already in use with your account. We don\'t do duplicates.<br>');
            } 

        }

        else {
            throw new Exception('We faced some unwanted errors. Please try once again.<br>');
        }

    }

    public static function check_details_from_db($link, $user_email){
        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        if(!filter_var($link, FILTER_VALIDATE_URL)){
            throw new Exception('Entered link is not in valid format. Please check it.<br>');
        } else 

            if(strlen($link) > 1000){
                throw new Exception('Entered link is too long to process.<br>');
            } else 

                //check links are in database and archived or not
                self::check_archived_link($link, $user_email) ;

    }

    public static function gen_code(){
        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        //generate random code for shorten url

        $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
        $len = 6;
        $numrows = 1;
        $code = empty($code);

        while($numrows != 0){
            for($i = 0; $i <= $len; $i++){
                $rand = rand()% strlen($charset);
                $temp = substr($charset, $rand, 1);
                $code .= $temp;
            }

            $find = $db->query("SELECT `id` FROM `url_table` WHERE BINARY `code` = '$code'");
            $numrows = $find->num_rows;
        }
        return $code;
    }

    public static function insert_link($link, $user_email, $blah){

        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        try {
            self::check_details_from_db($link, $user_email);
        } 
        catch(Exception $e) {
            echo '<b>Message:</b> ' .$e->getMessage();
        }

        if(empty($e)){

            $code = self::gen_code();
            if(strlen($code) > 7){
                $code = '';
                $code .= "";
                $code .= self::gen_code();


            }

            $ip = $_SESSION['ip'];
            $date = time();
            $site_name = $site;

            $hash_string = $user_email.'/'.$date.'/'.$ip.'/'.$code;
            $hash  = $blah->encode($hash_string);

            $insert_link_query = "INSERT INTO `url_table` (`url`, `site`, `code`, `u_email`, `create_time`, `ip`, `hash`, `archived`) 
                                                                  VALUES ('$link', '$site_name', '$code', '$user_email', '$date', '$ip', '$hash', '0')";


            if($result = $db->query($insert_link_query)){
                if($result AND $db->affected_rows > 0){
                    echo '<b>Message:</b> Successful! Check your all links at right side. You can manage all links from the same table.<br>';
                    $refresh_url = $site.'user/v1/home';                        
                } else {
                    echo '<b>Message:</b> We got an error. Please try again.<br>';
                }
            } else {
                echo '<b>Message:</b> Ooopps! We got an error. Please try again.<br>';
            }  
        }


    }

    public static function new_shorten_link($post_links){
        include $_SERVER['DOCUMENT_ROOT'].'/db.php';

        $cool = $db->real_escape_string(htmlentities($post_links['cool']));
        $user_email = $_SESSION['email'];
        include $_SERVER['DOCUMENT_ROOT'].'/encryption.php';
        $blah = new Encryption; 
        foreach($post_links['link'] as $links){
            $link = $db->real_escape_string(htmlentities($links));

            if(!empty($link)){
                $code = '';
                self::insert_link($link, $user_email, $blah);
            }
        }   
    }

}

?>
add_url.php

<form method="post" enctype="multipart/form-data" style="padding:10px;" autocomplete="off" >
     <input type="url" style="padding:5px; margin:5px; width:90%" name="link[]" placeholder="Insert link to make shorten url." required />
     <input type="url" style="padding:5px; margin:5px; width:90%" name="link[]" placeholder="Insert link to make shorten url." />
     <input type="url" style="padding:5px; margin:5px; width:90%" name="link[]" placeholder="Insert link to make shorten url." />
     <input type="url" style="padding:5px; margin:5px; width:90%" name="link[]" placeholder="Insert link to make shorten url." />
     <input type="url" style="padding:5px; margin:5px; width:90%" name="link[]" placeholder="Insert link to make shorten url." />
     <input type="hidden" name="cool" value="<?php echo $_SESSION['CSRF_TOKEN'];?>" />
     <button style="padding:5px; margin:5px; float:right;" type="submit" class="btn btn-small btn-success" name="shorten" value="sortn">Shorten</button>
     </form>
     <div id="msgDiv" style="padding:10px;">
     <?php


     if(isset($_POST['shorten']) AND ($_POST['shorten'] == 'sortn')){

         $post_links = $_POST;

         include 'usrShortenLink.php';
         Shorten::new_shorten_link($post_links);
         echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"3; url=$site\">";

     }
     ?>
     </div>

公共静态函数gen_code(){
包括$_SERVER['DOCUMENT_ROOT']./core/db_config_inc.php';
//为缩短url生成随机代码
$charset='abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzo123456789-"';
$len=6;
$numrows=1;
$code='';
而($numrows!=0和strlen($code)<8){
对于($i=0;$i查询(“从`url\u表`WHERE BINARY`code`='$code`中选择`id`”);
$numrows=$find->num\u行;
}
返回$code;
}

试试这个……这会帮你……;)

我认为,
$code=empty($code)做你认为它做的事。它将
$code
设置为true或false,这取决于它是否已为空。
$code.=''
的目的是什么,尤其是在设置
$code=''
之后?只是再次检查@Barmar…那么我该怎么办?你能帮我吗?你已经接受了答案。这不意味着你在他的帮助下解决了问题吗?你会建议如何解决它们吗?
public static function gen_code(){
            include $_SERVER['DOCUMENT_ROOT'].'/core/db_config_inc.php';

                    //generate random code for shorten url

                    $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
                    $len = 6;
                    $numrows = 1;
                    $code = '';

                    while($numrows != 0 AND strlen($code) < 8 ){
                        for($i = 0; $i <= $len; $i++){
                            $rand = rand()% strlen($charset);
                            $temp = substr($charset, $rand, 1);
                            $code .= $temp;
                        }

                        $find = $db->query("SELECT `id` FROM `url_table` WHERE BINARY `code` = '$code'");
                        $numrows = $find->num_rows;
                    }
        return $code;
    }