php 如何使用php检查$_GET['id']是否已设置且不为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3444048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether $_GET['id'] is set and it's not empty using php
提问by Dhruv Kumar Jha
Here's a php code
这是一个php代码
if(isset($_GET['id'])) {
//do something
} else {
redirect('index.php'); //redirect is a function
}
Now if id is set (ex: index.php?id=12) then the action is performed but if id is not set (ex: index.php?id=) this shows an error, how to overcome this...??
现在,如果 id 已设置(例如:index.php?id=12),则执行操作,但如果 id 未设置(例如:index.php?id=),则会显示错误,如何克服此问题...? ?
How to determine id is an integer and it's not empty and then perform the specific action....
如何判断id为整数且不为空然后执行具体动作....
Edited
已编辑
Thank you all for your answers but i am still getting that error...
谢谢大家的回答,但我仍然收到那个错误......
if(isset($_GET['id'])) { // I implemented all these codes but still....
$user= User::find_by_id($_GET['id']);
// Database Classes Fetches user info from database
}
else {
redirect('index.php'); //redirect function
}
If the id is 1 or greater than 1 then the script executes perfectly. ( index.php?id=1 )
如果 id 为 1 或大于 1,则脚本完美执行。( index.php?id=1 )
But id i set id as noting i get an error i..e index.php?id=
但是 id 我将 id 设置为注意我收到一个错误,即 index.php?id=
The code should automatically redirect user to the index.php page instead of showing an error.....
代码应该自动将用户重定向到 index.php 页面而不是显示错误.....
ERROR : Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
错误:数据库查询失败:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“LIMIT 1”附近使用的正确语法
回答by poke
You should check both the set-status and the content:
您应该检查设置状态和内容:
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) )
// ....
Note that you should notuse the value simply as it is and work with it. You should make sure that it only contains values you expect. To check for an integer, and even better to work with an integer from that on, do something like that:
请注意,您应不使用简单,因为它是值和工作吧。您应该确保它只包含您期望的值。要检查整数,甚至更好地使用整数,请执行以下操作:
$id = ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? intval( $_GET['id'] ) : 0;
if ( $id != 0 )
// id is an int != 0
else
redirect('index.php');
回答by ThoKra
What does the error tell you?
错误告诉你什么?
But this would check if it's set, and if it's an integer
但这会检查它是否已设置,以及它是否是整数
if(isset($_GET['id']) && ctype_digit($_GET['id']) && intval($_GET['id']) > 0)
Or check out ctype_digit
或查看ctype_digit
回答by zerkms
if (!empty($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT))
or
或者
if (!empty($_GET['id']) && ctype_digit($_GET['id']))
回答by Joseph
How to determine id is an integerand it's not emptyand then perform the specific action....
如何确定ID是一个整数并且它不为空,然后执行特定操作....
if (!empty($_GET['id']) && (intval($_GET['id']) == $_GET['id'])) {
//do something
} else {
redirect('index.php'); //redirect is a function
}
The above may not be the best solution, but it should work for what you need. It will not allow the id to be 0, and will require it to be an integer.
以上可能不是最好的解决方案,但它应该适合您的需要。它不允许 id 为 0,并要求它是一个整数。
回答by jolt
Well... If you're setting it up as an integer type variable, then you could do also this:
嗯...如果你将它设置为整数类型变量,那么你也可以这样做:
if($_GET['id'] && gettype($_GET['id']) == 'integer'){
#do something
}else{
#do something else
}
回答by KMK
if(isset($_GET['id']) && ctype_digit($_GET['id']) && is_numeric($_GET['id']) && $_GET['id']>0) {
$user= User::find_by_id($_GET['id']);
}
else {
redirect('index.php'); //redirect function
}
Try this and this code full pill your requirement.
试试这个和这个代码完整的药丸你的要求。
回答by Chad Scira
It seems like you're just having an issue with the ?id throwing a true when you want to check if its set. Thats because it is set, but not set to a value that you would like to use.
当您想检查它是否设置时,似乎您只是遇到了 ?id 抛出 true 的问题。那是因为它已设置,但未设置为您想要使用的值。
I'm assuming the userids have to be > 0 so just do this
我假设用户 ID 必须 > 0 所以就这样做
if (isset($_GET['id']) && $_GET['id'] > 0) {
$user= User::find_by_id($_GET['id']);
} else {
redirect('index.php');
}
Also you're prolly best cleaning the id before processing it so
此外,您最好在处理之前清理 ID,以便
$userID = isset($_GET['id']) && $_GET['id'] > 0 ? (int) $_GET['id'] : 0;
if ($userID) {
$user= User::find_by_id($userID);
} else {
redirect('index.php');
}
Theres a lot of ways to do it, personally I prefer this. It makes your code look a bit cleaner.
有很多方法可以做到,我个人更喜欢这个。它使您的代码看起来更简洁。
回答by EhsanSia
You'll have to check multiple times for every case possible: what if id is not set or empty? what if id is set but it is not a digit? what if 'id' key does not exist at all in GET request?
对于每种可能的情况,您必须多次检查:如果 id 未设置或为空怎么办?如果设置了 id 但它不是数字怎么办?如果 GET 请求中根本不存在“id”键怎么办?
I had similar situation. I was sending two vars with GET, one digit: 'id' and another one was string which was a name: 'state' ... I solved it by checking all possible conditions.
我有类似的情况。我用 GET 发送了两个变量,一个数字:'id',另一个是字符串,它是一个名称:'state'......我通过检查所有可能的条件来解决它。