If语句:如何拉第二个GET变量

编程入门 行业动态 更新时间:2024-10-27 02:21:10
本文介绍了If语句:如何拉第二个GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何得到这个拉我的第二个变量? (我已经有一个开关设置)

< body id =<?php if(!isset($ _ GET ['page'])){echohome;} else {$ _GET ['page']; echo $ page ;}?>>

我有一个 switch 语句, index.php?page = ##### pre>

我刚将这部分加入到开关中:

index.php?page = ####& section = #####

现在,如果我在page = photos上,我的代码就会变成:

< body id =photos>

我需要这样做,如果任何链接上有像这样的sections变量<

使用相同的ID:

< body id =照片>

解决方案

将PHP代码移出主体的id属性以提高可读性,并使用 else if 。通过消毒或验证来自 $ _ GET 的输入,确保您的代码不易被注入。例如:

<?php 函数isValidID($ x){ return preg_match(' /^[AZ][-_.A-Za-z0-9]$/i',$ x); } if(isset($ _ GET ['section'])&& isValidID($ _ GET ['section'])){ $ bodyID = $ _GET ['部分']; } else if(isset($ _ GET ['page'])&& isValidID($ _ GET ['page'])){ $ bodyID = $ _GET ['page']; } else { $ bodyID ='home'; } ?> ... < body id =<?php echo $ bodyID;?>>

另外,

<?php 函数isValidID($ x){ return preg_match('/ ^ [AZ] [-_ .A-Za-z0-9] $ / i', $ X); } $ bodyID ='home'; foreach(array('section','home')as $ key){ if(isset($ _ GET [$ key])&& isValidID($ _ GET [$ key])) { $ bodyID = $ _GET [$ key]; 休息; } } ?> ... < body id =<?php echo $ bodyID;?>>

在这种情况下,我会使用第一个展开版本。如果您必须检查更多输入键,请使用基于循环的方法。

如果您决定同时需要页面和放大器,你可以尝试如下所示:

<?php 函数isValidID($ x){ return preg_match('/ ^ [AZ] [-_。A-Za-z0-9] $ / i',$ x); } if(isset($ _ GET ['page'])&& isValidID($ _ GET ['page'])){ $ bodyID = $ _GET ['页']; } else { $ bodyID ='home'; } if(isset($ _ GET ['section'])&& isValidID($ _ GET ['section'])){ $ bodyID。='_'。 $ _GET [部分]; } ?> ... < body id =<?php echo $ bodyID;?>>

How do I get this to pull my 2nd variable? (I already have a switch setup)

<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">

I have a switch statement that pulls the pages from

index.php?page=#####

and I have just added this part to my switch:

index.php?page=####&section=#####

Right now, if I am on page=photos, my code ends up being:

<body id="photos">

I need to make it so that if any link has the "sections" variable on it like this page=photos&section=cars it uses the same ID:

<body id="photos">

解决方案

Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:

<?php function isValidID($x) { return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x); } if (isset($_GET['section']) && isValidID($_GET['section'])) { $bodyID = $_GET['section']; } else if (isset($_GET['page']) && isValidID($_GET['page'])) { $bodyID = $_GET['page']; } else { $bodyID = 'home'; } ?> ... <body id="<?php echo $bodyID; ?>">

Alternatively,

<?php function isValidID($x) { return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x); } $bodyID='home'; foreach (array('section', 'home') as $key) { if (isset($_GET[$key]) && isValidID($_GET[$key])) { $bodyID = $_GET[$key]; break; } } ?> ... <body id="<?php echo $bodyID; ?>">

In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.

If you decide you want both page & section in the ID, you can try something like:

<?php function isValidID($x) { return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x); } if (isset($_GET['page']) && isValidID($_GET['page'])) { $bodyID = $_GET['page']; } else { $bodyID = 'home'; } if (isset($_GET['section']) && isValidID($_GET['section'])) { $bodyID .= '_' . $_GET['section']; } ?> ... <body id="<?php echo $bodyID; ?>">

更多推荐

If语句:如何拉第二个GET变量

本文发布于:2023-07-29 08:21:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二个   变量   语句

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!