简体   繁体   English

如何在不断更改其值的同时访问多个PHP页面上的变量或变量值或变量

[英]How to access variable or variable value or variable on multiple PHP pages while continuously changing its value

I wanna to access in given scenario the the variable $i as it is an incremented varaible which change value after an order. 我想在给定的情况下访问变量$i因为它是一个递增的变量,在定单后会更改值。 So how can i reuse it this in any other page. 因此,我如何在其他任何页面中重复使用它。 Morever tell me the that how can i use any "variable/varaible last value" on multiple pages without the session. 而且我告诉我,如何在没有会话的情况下在多个页面上使用任何“变量/变量最后值”。 And will session update the variable value when its change , if yes than how ? 并且会话将在变量值更改时更新它,如果可以的话? and There are many methods to do that ? 并且有很多方法可以做到这一点?

 <?php include ('header.php'); 

    if(isset($_POST['order']))
    {
        $user_id  = $_SESSION['id'];
        $date      = date('Y-m-d H:i:s');
        $username =$_SESSION['username'];
        mysql_query("INSERT INTO tborder(user_id,`date`,username) VALUES('$user_id','$date','$username')") or die(mysql_error());
        $i = mysql_insert_id();
        if($i >0)
            for($l=0;$l<count($_POST['product_id']);$l++)
            {
                $product_id = $_POST['product_id'][$l];
                $quantity   = $_POST['quantity'][$l];
                $price      = $_POST['price'][$l];
                $discount   = $_POST['discount'][$l];
                $amount     = $_POST['amount'][$l];
                $username   = $_SESSION['username'];
                $user_id    = $_SESSION['id'];


        mysql_query("INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id) VALUES('$i','$product_id','$quantity','$price','$discount','$amount','$username','$user_id')")or die(mysql_error());
                header('location:order.php');
            }
    }

    ?>

Cookies! 饼干!

By storing the variable value in a cookie each time the value is changed, you can use the variable on multiple pages throughout the domain. 通过在每次更改值时将变量值存储在cookie中,可以在整个域的多个页面上使用变量。

The cookie expiration time needs to be considered though, if the value is going to be there for a whole session, then storing it in a session cookie (until the browser is closed) will be useful. 不过,如果要在整个会话中使用该值,则需要考虑cookie的过期时间,然后将其存储在会话cookie中(直到关闭浏览器)会很有用。

Hope it helps! 希望能帮助到你!

how can i use any "variable/varaible last value" on multiple pages without the session. 我如何在没有会话的情况下在多个页面上使用任何“变量/变量最后值”。

PHP variables are "lost" between page requests, unless you store them in a session. PHP变量在页面请求之间“丢失”,除非您将它们存储在会话中。 If you don't want to use a session to store data there are many alternatives: 如果您不想使用会话来存储数据,则有多种选择:

  • Pass values between pages as GET or POST variables. 在页面之间传递值作为GET或POST变量。 Any mistake and the value is "lost" again as it's not permanently stored. 任何错误,并且该值不会永久存储,因此会再次“丢失”。 The user can see and change the value themselves. 用户可以自己查看和更改值。
  • Store in a cookie . 存储在cookie中 Again the user can see and change the value themselves. 再次,用户可以自己查看和更改值。
  • Relational database , eg MySQL, PostgreSQL. 关系数据库 ,例如MySQL,PostgreSQL。 Create a table to store and query the value as needed across pages. 创建一个表来存储和查询跨页面所需的值。
  • Key-value store , eg Redis, memcache. 键值存储 ,例如Redis,内存缓存。 Define a key and read/write the value as needed. 定义一个键并根据需要读/写该值。

And will session update the variable value when its change 并会在会话更改时更新变量值

Only if you store it in the session and update it. 仅当您将其存储在会话中并更新时。 Each time you set a new value in the $_SESSION array it'll persist across pages as long as the session is alive. 每次您在$_SESSION数组中设置一个新值时,只要会话$_SESSION它就会在页面中保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM