基于会话的PHP查询

时间:2014-08-05 06:27:36

标签: php

我想知道如何在php中重启会话。当我在我的一个页面中使用下面给出的代码时,它将第一次起作用。当我刷新页面时,前一个会话继续。那么如何在页面重新加载时开始新的会话?感谢您的快速响应。包含整个代码以使事情更加清晰

<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="products-wrapper">
    <h1>Products</h1>
    <div class="products">
    <?php
    //current URL of the Page. cart_update.php redirects back to this URL
    $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

    $results = $mysqli->query("SELECT * FROM shop ORDER BY product_code ASC");
    if ($results) { 

        //fetch results set as object and output HTML
        while($obj = $results->fetch_object())
        {
            echo '<div class="product">'; 
            echo '<form method="post" action="cart_update.php">';           
            echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
            echo '<div class="product-info">';
            echo 'Price '.$currency.$obj->price.' | ';
            echo 'Qty <input type="text" name="product_qty" value="0" size="3" />';
            echo '<button class="add_to_cart">Add To Cart</button>';
            echo '</div></div>';
            echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
            echo '<input type="hidden" name="type" value="add" />';
            echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
            echo '</form>';
            echo '</div>';
        }

    }
    ?>
    </div>

<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["shop"]))
{
    $total = 0;
    echo '<ol>';
    foreach ($_SESSION["shop"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code"></div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php">Check-out!</a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}else{
    echo 'Your Cart is empty';
}

?>
</div>

</div>

</body>
</html>

5 个答案:

答案 0 :(得分:0)

尝试首先删除会话并重新开始

if(session_id() != '' || isset($_SESSION)) {
  session_destroy();
  session_start();
}
else {
  session_start();
}

答案 1 :(得分:0)

您可以在文件末尾使用session_destroy函数。

<?php
    session_start();
    include_once("config.php");

    /*your code*/

    session_destroy();
?>
  
    

session_destroy - 销毁注册到会话的所有数据

         

bool session_destroy(void)

  

http://php.net/manual/en/function.session-destroy.php

答案 2 :(得分:0)

您需要使用session_destroy();来销毁会话,然后才能开始新会话

答案 3 :(得分:0)

<?php
@session_destroy(); // destroy session if exists
session_start();
include_once("config.php");
?>

更新2:

<?php
if(isset($_SESSION))
{
    session_destroy(); // destroy session if exists
}

session_start();
include_once("config.php");
?>

更新3:

如果您想在使用后仅取消$ _SESSION [&#39; shop&#39;]

if(isset($_SESSION["shop"]))
{
  // your code

  unset($_SESSION['shop']); // unset your session after using it
}else{
    echo 'Your Cart is empty';
}

答案 4 :(得分:0)

您必须在活动结束后销毁会话。

<?php
session_start();
include_once("config.php");

//after doing your task with the session variable destroy session
   session_destroy();
?>