dtftao7249656 2011-08-18 18:11
浏览 14
已采纳

这个功能好吗? [关闭]

All my website functions are like the one i will paste below. i wanted to ask, has this function any possible bug ? will this function make the server load hard ? can you suggest anything better for my website ? there are a lot of pageviews at my web, so i need to get my data without loading hard my server.

the code ( this is used to get the single posts.)

function single($id = '') {
    $id = mysql_real_escape_string ($id);
    $sql = 'SELECT id,post_title,post_date,post_content FROM wp_posts WHERE id='.$_GET['id'].'  LIMIT 1';
    $res = mysql_query($sql) or die (mysql_error());    

if (mysql_num_rows($res) !=0):
    while ($row = mysql_fetch_assoc($res)) {

    //this filter the content from the database
    $mycontent = $row['post_content'];
    $mycontent = strip_tags($mycontent);
    $mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent); 
    $mycontent = htmlentities($mycontent);

    //this make possible  to show special characters on title
    $title = $row['post_title'];
    $title = htmlentities($title);

    //date format
        $old_date = $row['post_date'];            
    $old_date_timestamp = strtotime($old_date);
    $new_date = date('d.m.Y   H:i', $old_date_timestamp); 

    //get first post image
    $first_img = '';
    ob_start();
    ob_end_clean();
    $my1content = $row['post_content'];
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
    $first_img = "/img/default.png";
    }

    echo '
        <div class="single-header">
        <div class="single-title">'.$title.'</div>  
        <div class="single-tr"> '.$new_date.'</div> 
        </div><!-- single header -->
        <div class="single-print"></div><!-- print -->
        <div class="single-content">
        <div class="single-img">
        <img src="timthumb.php?src='.$first_img.'&amp;h=223&amp;w=395&amp;zc=1" alt="" />
        </div>
        <div class="single-text">'.$mycontent.' </div>
        </div> <!-- content -->
    '; //echo
}
    else:
        echo 'Dont exist';
    endif;
} // end

This is very important for me , please check it , any kind of help will be just great

Thank you a lot for reading this thread.

  • 写回答

1条回答 默认 最新

  • douangoo48863 2011-08-18 18:32
    关注
    1. Security: you are mysql-real-escaping the $id from the function call, but including $_GET['id'] into the query…
    2. There is nothing between ob_start(); and ob_end_clean();, so its useless
    3. I would make some minor changes just for readability:

    .

    // e.g. instead
    $title = $row['post_title'];
    $title = htmlentities($title);
    // do
    $title = htmlentities($row['post_title']);
    

    EDIT:

    As this seems to be a function for Wordpress, you may use the wpdb-class instead of using mysql-functions directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看