如果url中没有变量,则隐藏网页



我创建了一个表单,使用以下代码从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.0n";  
$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: PHP4n";  
$headers .= "X-Priority: 3n"; //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_partsn";  
$message .= "Content-Type: text/plain; charset="iso-8859-1"n";  
$message .= "Content-Transfer-Encoding: quoted-printablen";  
$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>

用下面的代码启动php页面

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

这应该能解决你的问题

最新更新