Solved

Help avoiding duplicates when adding to PHP Session

Posted on 2014-11-24
1
117 Views
Last Modified: 2014-11-24
When trying to add one or more arrays to my Session array, I run into a problem where the added array is duplicated on a page reload. I've tried a few different things to avoid this, but can not seem to find the solution.

Is it possible to somehow count how many times the page has been refreshed so that $discountedItems is only added once to the Session, regardless of how many times the page get's refreshed?

$discountedItems = array(
                    'name' => 'discount', 
                    'quantity' => '1', 
                    'price' => '-35.00',
                    );

array_push($_SESSION['items'],$discountedItems);

Open in new window

0
Comment
Question by:jej07
1 Comment
 
LVL 9

Accepted Solution

by:
teebon earned 500 total points
Comment Utility
Try the following :

session_start();

if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}


$discountedItems = array(
                    'name' => 'discount', 
                    'quantity' => '1', 
                    'price' => '-35.00',
                    );

if($_SESSION['count'] == 0)
{
  array_push($_SESSION['items'],$discountedItems);
}

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now