Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help avoiding duplicates when adding to PHP Session

Posted on 2014-11-24
1
Medium Priority
?
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
teebon earned 2000 total points
ID: 40463674
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

722 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