dongxuan1660 2014-11-11 19:05 采纳率: 100%
浏览 12
已采纳

php在输入无效网址后将用户移至404页面

I'm designing a website but I know if the user enters a wrong character into my url, a not found page will open for him . and I know it can be a way to hack my website. What should I do for that? for example if the user enters a ' into my url like this:

http://example.com/article.php?id=585'

He move to a not found page which I have designed it or move to the first page or the last page he was in. Thanks.

  • 写回答

3条回答 默认 最新

  • duanfei9278 2014-11-11 19:20
    关注

    You have to take 2 things into consideration:

    1. Handling non-existent files
    2. Handling non-existent article ids

    Here's how to handle each case:

    1) Create an .htaccess file and place it in your website root folder:

    RewriteEngine on
    ErrorDocument 404 /error.php # change this to your own 404 file path
    

    2) Open the articles.php file and add this to the top (right after checking if your ID exists)

    if(!valid_id($id)) {
        //if you have php 5.3- use this
        header('HTTP/1.1 404 Not Found');
    
        //if you have php 5.4+ use this
        //http_response_code(404);
    
        include('error.php'); //change this path to your own 404 file
        die();
    }
    

    Obviously, valid_id() is just a function example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题