基于会话的PHP查询

基于会话的PHP查询,php,Php,嗨,我想知道如何用php重新启动会话。当我在我的一页中使用下面给出的代码时,它将第一次起作用。刷新页面时,上一个会话将继续。那么,当页面重新加载时,我如何启动新会话呢?感谢您的快速响应。包含了完整的代码,以使事情变得清晰。 <?php session_start(); include_once("config.php"); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-T

嗨,我想知道如何用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>

购物车
产品
你的购物车

首先尝试删除会话,然后重新启动

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

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

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

    /*your code*/

    session_destroy();
?>

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

bool会话_销毁(无效)


您需要使用
session_destroy()
要销毁会话,然后可以启动新会话,必须在事件发生后销毁会话

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

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


警告:会话\u destroy():试图在线销毁C:\wamp\www\zeromilez.com\Shop.php中未初始化的会话2@SKRocks是的,请查看我的评论所有你的建议似乎都不起作用那么你为什么要使用
会话
?如果您希望在页面重新加载时销毁变量?请将此代码放在文件的末尾。
@SyedQarib当我尝试您的代码时,当我尝试添加其他产品时,购物车将清空。不,那么我猜这与会话无关。您遇到了什么问题?会话仍在继续。查看我的完整代码并让我知道您是否只想取消设置
$\u SESSION['shop']
在使用它之后当我这样做并从购物车中删除一个项目时,没有页面加载,页面保持空白白色。错误表示您正在尝试销毁未初始化的sessionOk。您的会话超时时间是多少??多少分钟/小时?是否要在每次页面加载时启动新会话??
if(isset($_SESSION["shop"]))
{
  // your code

  unset($_SESSION['shop']); // unset your session after using it
}else{
    echo 'Your Cart is empty';
}
<?php
session_start();
include_once("config.php");

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