简体   繁体   English

删除网址末尾的字符

[英]Remove character at the end of url

1) I have url like this : http://example.com/post.php?id=1234 And inside : my article 1)我有这样的网址: http : //example.com/post.php?id=1234里面:我的文章

2) but for this url http://example.com/post.php?1234somewords It's also work, i see my article 2),但对于此网址http://example.com/post.php?1234somewords也可以,我看到了我的文章

3) and for this url http://example.com/post.php?somewords I have good 404 page error 3),并为此网址http://example.com/post.php?somewords我有很好的404页面错误

Question is : how could i have 404 error for the 2) url ? 问题是:2)url如何出现404错误? (alternative question : how could i redirect "1234somewords" to "1234" ?) (另一个问题:我如何将“ 1234somewords”重定向到“ 1234”?)

php mysql query inside post.php is : 在post.php中的php mysql查询是:

require_once('conn_sql.php');
$post = $_GET['post'];
$nQuery = mysqli_query($conn, "SELECT * FROM `post` WHERE post_id = '$post'");
$res = mysqli_fetch_array($nQuery);

It seems that the query "post=1234somewords" works, and this is not what i want. 似乎查询“ post = 1234somewords”工作,这不是我想要的。 however, if i search "post=1234somewords" in phpmyadmin, this not works, and this is what i want ! 但是,如果我在phpmyadmin中搜索“ post = 1234somewords”,则此方法无效,这就是我想要的!

What is the problem with my code ? 我的代码有什么问题?

this happen because mysql use the beginning part of the string as a valid id .. (this i related to the implic data conversion performed by mysql) you should check if your parameter are valid number before perform the query 发生这种情况是因为mysql使用字符串的开头部分作为有效ID ..(这与mysql进行的隐式数据转换有关),您应该在执行查询之前检查参数是否为有效数字

you could try removing the not numeric value from the string 您可以尝试从字符串中删除非数字值

$result  = preg_replace("/[^0-9]/", "", $_GET['post']; );  

if (is_numeric( $result))  {

  $nQuery = mysqli_query($conn, "SELECT * FROM `post` WHERE post_id = '$post'");
  $res = mysqli_fetch_array($nQuery);

} else {
  ......

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM