Bhavesh
Bhavesh

Reputation: 4677

How to maintain global session in 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?

Upvotes: 0

Views: 837

Answers (3)

Serge
Serge

Reputation: 1601

As I did in one project: a shopping cart is updated in two places on each change: on the server and in the loaded page, dynamically.

A click on cart item's +/- button to change its quantity would fire a JavaScript event, that does the following:

  1. makes an ajax call to server to let it know the cart's content has changed;
  2. updates the displayed cart in the loaded page.

Have a look at a JS library like jQuery or Mootools, for instance. They make JS scripting a lot easier than writing code from scratch! There are lots of other similar libraries too.

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174967

You can use JavaScript's AJAX calls to make requests to the server without a page refresh.

Take note however, that some browsers don't support (or have disabled) JavaScript. So make sure your script still works even with a page refresh.

Upvotes: 0

Guillaume Poussel
Guillaume Poussel

Reputation: 9822

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.

Upvotes: 2

Related Questions