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?