我如何得到这个拉我的第二个变量? (我已经有一个开关设置)
< 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=####§ion=#####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§ion=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变量
发布评论