dongyou5271 2012-04-21 13:11
浏览 59

如何在PHP中维护全局会话?

I'm currently working with an application in PHP where I need to maintain a shopping cart. Everything is working well.

When an item is added to the cart, I need to display a message on the header of each page something like this "1 item in the shopping basket", if the cart contains one item.

When additional items are added (or deleted), the message should change accordingly without a page refresh like "2 items in the shopping basket" and so on.

What problem I'm facing is that I need to refresh the page when some items are deleted or added to the cart (then and only then I can see the updated items on the header of the page)

For example, let's say that the cart is right now containing 2 items, now one additional item is added, the message on the header will still display "2 items in the shopiing basket" instead of displaying "3 items in the shopping basket" until the page is refreshed.


In Java, we have SessionListener as follows.

package sessionListener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener()
public class SessionListener implements HttpSessionListener
{
    private HttpSession session=null;        

    @Override
    public void sessionCreated(HttpSessionEvent se)
    {
        session=se.getSession();
        //Use this session            
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se)
    {

    }
}

I could have used the sessionCreated() method to suit the requirements because it's the method which is excecuted only once when the session is first created.

Similarly in .NET, we have Global.asax application file such as

<%@ Application Language="C#" %>

<script runat="server">

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

    }

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown            
    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        Session.Add("Message", SomeValue);            
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

        Session.Abandon();

    }

</script>

I could have used the void Session_Start(object sender, EventArgs e) method to suit the requirements because this is the method that is called exactly once when a session begins (once for each user's session).

But in PHP, I can't find the similar concept that is executed only once when a new session is created (I'm right now not using any framework in PHP).

If so then how can I display the above specified message on the header of each page when the status of the shopping cart is updated without a page refresh at all?

  • 写回答

3条回答 默认 最新

  • dqgg25493 2012-04-21 13:21
    关注

    To change a part of your page without refresh, you will need a client-side language. Javascript could help you, especially with AJAX calls : you call a PHP script which returns the item count in your shopping cart.

    You can call this each X minutes to refresh item count even if the user open multiple tabs in the browser.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法