PHP 5 / Basket Class (OOP)

Shopping is fun and thanks to Ecommerce shop offers are countless. Nevertheless how to present and handle a shop leaves only a few serious possibilities. One of them is a session solution with classes (OOP). This shop example can do the following: cookie control, presentation of products (see screenshots below), basket handling (add, remove, increase, reduce quantity, zero result handling, subtotal and total), session check plus detection of string manipulations). In the first step we check the configuration of the browser (cookies on/off) then we set the user session and present our shop. The handling of the orders is done by the basket class. That for we need two sessions: one for the basket with the orders and one for the quantities of orders (which are identified through moredimensional arrays and the product id). In the next step we control, if the user takes the same order more then once. Whith a control variable (+/-) and a foreach loop we increase or reduce the quantity for each chosen product. The shopping basket is a combination between a while-list-each- and a database-query - so calculations ar easy done. If quantity result is zero, order will be automatically removed from basket. Security feature: Evil visitors will be recognized by a explode script that determines string manipulations (in this example a table name, but also very usefull for other strings ...), closes the shop and registers the ip address - after three violations the user will be informed that his ip address has been taken into a database table.

The scripts snippets you see here (the complete script is downloadable) are only a part of the complete application. The classes, sql, css and html pages and other stuff is all prepared for your convenience. Best regards Claudio Biesele


PHP code for the product presentation in HTML file:

$cssformat = "title";
$showproducts = new showResellerProducts();
$showproducts->listproducts($produktid,$produktname,$sfr,$eu,$showtab,$cssformat);
$showproducts->showtab
;

Part of class code (remove order):

function removeproducts($id,$produktid,$produkt,$bestellmenge,$message)
{
    $this->id = $id;
    $this->produktid = $produktid;
    $this->produkt = $produkt;
    $this->bestellmenge = $bestellmenge;
    $this->message = $message;
if (!isset($this->produktid) ||
    !in_array($this->produktid, $_SESSION['warenkorb'])) {
    $this->message = "Either this product is not in your basket
    or you have not chosen one.";
    echo $this->message;
} else {
    foreach ($_SESSION['warenkorb'] as $this->id => $this->produkt) {
    if ($this->produkt == $this->produktid) {
    unset($_SESSION['warenkorb'][$this->id]);
    unset($_SESSION['menge'][$this->id]); }}
    $this->message = "Your order has been removed from basket.";
    echo $this->message;
    $this->break = "<br/>";
    echo $this->break; }
}


Part of intruder defense class:

$this->res1 = explode(" ", $this->tabprodukt);
$this->res2 = explode("=", $this->tabprodukt);
$this->res3 = explode("OR", $this->tabprodukt);
$this->res4 = explode("or", $this->tabprodukt);
$this->allres = array_merge($this->res1, $this->res2, $this->res3, $this->res4);

foreach($this->allres as $this->key=>$this->wert)
{
    if($this->key > '3')
    {
        $this->error = '1';
    }
}


Picture basketOOP_1.gif
1. Step: Conventional presentation of the shopping area (data from database).
Picture basketOOP_2.gif
2. Step: Confirmation: The order has been taken and recognized by the session.
Picture basketOOP_3.gif
3. Step: Basket: Order infos are taken from session and database.
Picture basketOOP_4.gif
4. Step: Confirmation: The order has been removed from the session.
Picture basketOOP_5.gif
5. Step: Confirmation: The quantity has been increased.
Picture basketOOP_6.gif
6. Step: Confirmation: The quantity has been reduced.
Download the full script at www.fastproject.ch. The downloaded script is free from Copyright restrictions, Zurich, 22th of November 2015.
Donate with PayPal:
placeholder