【问题标题】:Hide web page if no variable present in the url如果 url 中不存在变量,则隐藏网页
【发布时间】:2011-10-19 10:47:36
【问题描述】:

我创建了一个表单,它使用以下代码从 url 中提取变量:

<?php $id = $_GET['id']; echo $id; ?>

以这个链接为例 (http://www.mydomain.co.uk/customerform.php?id=12) 它将在网页上显示 12。

我想知道的是,如果 URL 中没有变量,例如 ?id=12 ,是否可以阻止网页加载?

在 php、javascript 中都可以吗?

所以如果您访问以下链接,网页将显示:

http://www.mydomain.co.uk/customerform.php?id=12

如果你访问了这个链接(没有变量),页面将不会显示:

http://www.mydomain.co.uk/customerform.php

感谢您的帮助。

以下更新...

我添加了 Krishnanunni 的代码,如果 url 中没有变量,它会成功阻止网页显示。但是添加此代码会阻止表单提交...

有没有办法在提交表单时保留 url 变量?

有人可以帮忙吗?谢谢

<body id="main_body" >
<div id="form_container">

<?  
/* only load page is variable is presnt in the URL */  
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
?>  
<?  
/* Mailer with Attachments */  

$action = $_REQUEST['action'];  
global $action;  

function showForm() {  
?>  
<form id="contact" class="appnitro" name="idform" enctype="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />  
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input name="to_email" type="hidden" id="to_email" value="myemail@domain.co.uk"/>
<div>
  <input id="invoice" name="invoice" class="element text medium" type="hidden" maxlength="255" value="<?php $id = $_GET['id']; echo $id; ?>"/> 
</div> 
<div>
        <input id="subject" name="subject" class="element text medium" type="hidden" maxlength="255" value="Enquiry Form - (Invoice: <?php $id = $_GET['id']; echo $id; ?>)"/> 
</div> 

      <label class="description" for="from_name">Name </label>
    <div>
        <input id="from_name" name="from_name" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 


    <label class="description" for="position">Position </label>
    <div>
        <input id="position" name="position" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 


    <label class="description" for="uemail"> Email </label>
    <div>
        <input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 


    <label class="description" for="phone"> Phone </label>
    <div>
        <input id="phone" name="phone" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 


    <input name="Reset" type="reset" class="contactform_small" value="Reset" />
      <input name="Submit2" type="submit" class="contactform_small" id="Submit" value="Submit" />


    </form>        
    <?  
}  

function sendMail() {  
if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!  
die ("<p>Oops!  You forgot to fill out the email address! Click on the back arrow to go back</p>");  
}  
else {  
$to_name = "MY BUSINESS";
$from_name = stripslashes($_POST['from_name']);  
$subject = Trim(stripslashes($_POST['subject'])); 
$to_email = $_POST['to_email'];
$EmailFrom = Trim(stripslashes($_POST['email'])); 

// get posted data into local variables

$position = Trim(stripslashes($_POST['position'])); 
$position2 = Trim(stripslashes($_POST['position2'])); 
$phone = Trim(stripslashes($_POST['phone'])); 
$invoice = Trim(stripslashes($_POST['invoice'])); 

//Let's start our headers  
$headers = "From: $EmailFrom<" . $_POST['email'] . ">\n";  
$headers .= "Reply-To: <" . $_POST['email'] . ">\n";  
$headers .= "MIME-Version: 1.0\n";  
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\";  boundary=\"----=MIME_BOUNDRY_main_message\"\n";  
$headers .= "X-Sender: $from_name<" . $_POST['email'] . ">\n";  
$headers .= "X-Mailer: PHP4\n";  
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal  
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";  
$headers .= "This is a multi-part message in MIME format.\n";  
$headers .= "------=MIME_BOUNDRY_main_message \n";  
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";  

$message = "------=MIME_BOUNDRY_message_parts\n";  
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";  
$message .= "Content-Transfer-Encoding: quoted-printable\n";  
$message .= "\n";  
/* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */  
$message .= "Invoice: ";
$message .= $invoice;
$message .= "\n";
$message .= "\n";
$message .= "My Business Enquiry Form";
$message .= "\n";
$message .= "\n";
$message .= "Contact Details: ";
$message .= "\n";
$message .= "\n";
$message .= "Name: ";
$message .= $from_name;
$message .= "\n";
$message .= "Position: ";
$message .= $position;
$message .= "\n";
$message .= "Email: ";
$message .= $EmailFrom;
$message .= "\n";
$message .= "Phone: ";
$message .= $phone;
$message .= "\n";
$message .= "\n";

// send the message  
mail("$to_name<$to_email>", $subject, $message, $headers);  
print "Form sent successfully";
}  
}  

?>
                          <?  
switch ($action) {  
case "send":  
sendMail();  
showForm();  
break;  
default:  
showForm();  
}  
?>

    <div id="footer">

    </div>
</div>
</body>

【问题讨论】:

  • 可以通过渲染不同的html,或者空白html,或者返回404等方式“隐藏”页面。

标签: php javascript url variables webpage


【解决方案1】:

使用此代码启动您的 php 页面

if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}

这应该可以解决您的问题

【讨论】:

  • 优秀。非常感谢您的快速解决方案
  • 您好,小问题 - 它阻止了我的表单提交。要遵循的代码...
猜你喜欢
  • 2011-09-11
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2013-09-27
相关资源
最近更新 更多