2013-06-28 106 views
-1

我正在製作一個由多個頁面組成的過程,我希望用戶只能通過點擊購物車頁面(showCart.php)中的按鈕才能進入「Checkout1.php」。這也是一個登錄檢查工作。PHP:只通過按鈕進入頁面?

checkOut1.php:

<article id="content"> 
    <h2>Check Out</h2> 

    <h3><span class="checkOut">1. Get Delivery Details</span> -> 2. Confirm Order -> 3. Make Payment -> 
4. Print Order Confirmation</h3> 

    <?php 
    if ((isset($_POST['submitShowCart'])) || (isset($_POST['returnCheckOut2'])) || 
    (isset($_POST['submitLogin']))) { 

     if ($_SESSION['login']=='valid') { 
     echo "<p>Please modify delivery details as necessary and continue the Check Out process:</p>"; 

    ?> 

     //Blah blah blah (Main Form Code) 

    <form method="post" action="showCart.php"> 
     <p> 
     <input type="submit" value="Return To Shopping Cart" name="returnCheckOut1"> 
     </p> 
    </form> 

    <br><p>* = Required Fields</p> 
    <p><a href="#" class="intLink">[Top]</a></p> 

    <?php 
    } 
    else { 
     echo "<p>Please login to continue the Check Out process.</p>"; 
    } 
} 
    else { 
    // display no access message 
    echo "<p>Cannot access this file directly - must come via the Check Out process.</p>"; 
    } 
    ?> 
</article> 

showCart.php:

<article id="content"> 
    <h2>Your Shopping Cart</h2> 

<?php 
if (empty($_SESSION['cart'])) { 
    echo "<p>Your shopping cart is currently empty.</p>"; 
} 
else { 
?> 

<!-- open form so that qtyOrdered field becomes an input element --> 
<form action="updateCart.php" method="post"> 

<!-- display headings for cart --> 
<table class="prod1"> 
    <tr> 
    <th>Product Name</th> 
    <th>Quantity On Hand</th> 
    <th>Quantity Ordered</th> 
    <th>Unit Price $</th> 
    <th>Extended Value $</th> 
</tr> 

<?php 
// set up starting value of grand total 
$grandTotal = 0; 

// read details from the cart session variable and display 
foreach($_SESSION['cart'] as $prodNbr=>$value) { 
    // extract data into separate variable 
    $prodName = $_SESSION['cart'][$prodNbr]['prodName']; 
    $price = $_SESSION['cart'][$prodNbr]['price']; 
    $qtyOnHand = $_SESSION['cart'][$prodNbr]['qtyOnHand']; 
    $qtyOrdered = $_SESSION['cart'][$prodNbr]['qtyOrdered']; 

    // do calculation for extended value 
    $extendedValue = $qtyOrdered * $price; 

    // accumulate the grand total of extended value column 
    $grandTotal = $grandTotal + $extendedValue; 

    // display fields in a table row 
    echo "<tr>"; 
    echo "<td>$prodName</td>"; 
    echo "<td class='right'>$qtyOnHand</td>"; 
    echo "<td class='right'><input type='text' value='$qtyOrdered' size='2' name='qtyToBuy[$prodNbr]' /></td>"; 
    echo "<td class='right'>$price</td>"; 
    echo "<td class='right'>".number_format("$extendedValue",2)."</td>"; 
    if ($qtyOrdered == $qtyOnHand) { 
     echo "<td><strong>No further stock available</strong></td>"; 
    } 
    echo "</tr>"; 
} // end of foreach loop 

    // display the grand total 
    echo "<tr><td colspan='4' class='right'><strong>Grand Total : </strong></td><td class='right highlight'>" .number_format("$grandTotal",2) ."</td></tr>"; 

echo "</table>"; 
echo "<br>"; 
echo "<input type='submit' value='Update Cart'>"; 
echo "</form>"; 
echo "<br>"; 
echo "<h3>Enter a new quantity ordered OR 0 to remove the item from your cart.</h3>"; 
?> 

<input type="button" value="Check Out" onclick="javascript:location.replace('checkOut1.php')" />  
<input type="button" value="Continue Shopping" onclick="javascript:location.replace('catalogue.php')" /> 

    <?php 
    } 
    ?> 
</article> 

編輯:我想checkout1檢查,如果用戶來到從showCart該頁面,代碼我現在已經沒有按」檢查並總是給我「不能直接訪問此文件 - 必須通過簽出過程。」信息。

+0

你想要什麼? – Jerska

+0

檢查'$ SERVER ['HTTP_REFERER']' – alwaysLearn

回答

1

如果你想確保用戶來自showCart.php到checkout.php添加這checkout.php:

if(basename($_SERVER['HTTP_REFERER'])=="showCart.php"){ 
//do your code of checkout.php 
} 
else{ 
//show error message and redirect the page. 
} 
+0

這讓它工作,謝謝。我在結賬時更換了第一個「IF」線。 –

1

當用戶點擊按鈕,您可以設置一個SESSION變量,並在目標頁面上,您可以檢查該SESSION變量是否設置。如果設置,則顯示其他頁面不顯示。

+0

事情是我有這個工作之前使用幾乎沒有使用SESSION變量和這樣的代碼。如果可能的話,而不是使用這個帖子我有。不過我會研究SESSION變量。編輯:我遵循Drarkayls解決方案,它的工作,你是正確的會話變量。我的壞:v –