[英]PHP Query in my URL
I am trying to write a peace of code that will change the title of the page based on what is in the url query... 我正在尝试编写一个代码的和平,将根据url查询中的内容更改页面的标题...
?page_id=62&action=register
so 所以
if(action='register'){
do this
}else{
do this
}
how would I write this? 我怎么写这个? I have never dealt with this before
我以前从未处理过这个问题
if(isset($_GET['action']) AND $_GET['action'] == "register")
{
// your code
}
Access URL parameters using $_GET
: 使用
$_GET
访问URL参数:
$page_id = (int)$_GET['page_id'];
$action = htmlspecialchars($_GET['action']);
if($action == 'register') {
echo 'action: '. $action .', page_id: '. $page_id;
}
Read more about GET
& POST
here . 在这里阅读更多关于
GET
和POST
信息 。 If you want to check if those variables are set use isset eg isset($_GET['page_id'])
. 如果要检查是否设置了这些变量,请使用isset eg
isset($_GET['page_id'])
。
Be careful though, it's easy to create range of vulnerabilities this way, escape/validate those variables (eg htmlspecialchars used it my code). 但是要小心,以这种方式创建各种漏洞很容易,逃避/验证这些变量(例如htmlspecialchars使用了我的代码)。
To read out the "action" you need the $_GET variable. 要读出“动作”,你需要$ _GET变量。 You can use it with
$_GET['action']
. 您可以将它与
$_GET['action']
。
You should ever use isset to ask if the variable is set or not. 您应该使用isset来询问是否设置了变量。 With this you can prevent errors.
有了这个,你可以防止错误。
if(isset($_GET['action']) && $_GET['action'] == 'register'){
do this
}else{
do this
}
Another example: when you ask if the action is set and after that you can ask if action == register or action == login or whatever 另一个例子:当你询问是否设置了动作时,你可以询问动作==注册或动作==登录或其他什么
if(isset($_GET['action'])){
switch($_GET['action']) {
case 'register':
//do this
break;
case 'login':
//do this
break;
}
}
Something like this: 像这样的东西:
$action = $_GET['action'];
if ($action == "register") {
echo("<title>Register</title>");
}
placed in between your and tags in your php script 放在你和php脚本中的标签之间
Try something like: 尝试类似的东西:
$title = (isset($_GET['action']) && $_GET['action'] == 'register') ? 'Register' : 'Login';
And in your HTML: 在你的HTML中:
<title><?=$title?></title>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.