[英]How can I combine two If Statements in PHP to block access to a URL?
我需要阻止访问所有不包含&uuid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
URL
x
是随机的,可以包含数字和文本。
最终代码需要同时包含两个验证: if ($context->data->ticket->ohanah_event_id)
和URL检查。
此刻使用的代码是:
<?php
/**
* @package Ohanah
* @copyright Copyright (C) 2012 - 2016 Beyounic SA. All rights reserved.
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://www.beyounic.com
*/
class ComOhanahViewTicketHtml extends ComOhanahViewHtml
{
protected function _fetchData(KViewContext $context)
{
parent::_fetchData($context);
if ($context->data->ticket->ohanah_event_id) {
$doc = JFactory::getDocument();
$translator = $this->getObject('translator');
$doc->setTitle($translator->translate('COM_OHANAH_TICKET') . ' - ' . $context->data->ticket->event->title);
}
}
}
class ComOhanahViewTicketHtml extends ComOhanahViewHtml
{
protected function _fetchData(KViewContext $context)
{
parent::_fetchData($context);
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if(isset($context->data->ticket->ohanah_event_id) && strpos($url, '&uuid=') == true) {
//here $url is your url
$doc = JFactory::getDocument();
$translator = $this->getObject('translator');
$doc->setTitle($translator->translate('COM_OHANAH_TICKET') . ' - ' . $context->data->ticket->event->title);
}
}
}
我想这就是你的想法
这将是这样的..如果您给我更多uuid格式的精度,我可以使用更好的正则表达式进行更新。 例如,如果该示例显示x
的精确数字,以-
分隔,则正则表达式将为'/^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$/'
。 (请注意,这些正则表达式不支持带重音符号的字符)
class ComOhanahViewTicketHtml extends ComOhanahViewHtml
{
protected function _fetchData(KViewContext $context)
{
parent::_fetchData($context);
if(!isset($_GET['uuid']) || !preg_match('/^[A-Za-z0-9-]*$/', $_GET['uuid'])){
exit('URL NOT ALLOWED!');
}else{
if ($context->data->ticket->ohanah_event_id) {
$doc = JFactory::getDocument();
$translator = $this->getObject('translator');
$doc->setTitle($translator->translate('COM_OHANAH_TICKET') . ' - ' . $context->data->ticket->event->title);
}
}
}
}
请注意,如果尚未发送标头,则可以在标头中添加准确的禁止错误header('HTTP/1.0 403 Forbidden');
(简单的版本,您可以找到更好的接受HTTPS和所有协议的版本)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.