1

I am trying to developing a ecommerce website. But My problem is in product purchase confirmation page.It show this type of message: (Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\online shop\dataAccessLayer\dalSession.php on line 6) I need to some suggestion how can I solve this issue. Here is my code.

This is my dalSession.php page:

<?php

class Session
{
public static function Start(){
    session_start(); //line 6
}

public static function Set($key , $value){
    $_SESSION[$key] = $value;
}

public static function Get($key){
    if (isset($_SESSION[$key])) {
        return $_SESSION[$key];
    }
    else{
        return false;
    }
}

public static function Stop(){
    session_destroy();
    header("Location:user/login.php");
}

public static function StopA(){
    session_destroy();
    header("Location:../user/login.php");
}

public static function StopB(){
    session_destroy();
    header("Location:login.php");
}


public static function Check()
{
    self::Start();
    //echo $this->user_id;;
    if (self::Get("Mlogin")==false) 
    {
        self::Stop();
        header("Location:login.php");
    }
}

public static function CheckA(){
    self::Start();
    if (self::Get("Alogin")==false) {
       self::StopA();
       header("Location:../user/login.php");
    }
}

public static function CheckAll()
{
    if (self::Get("Mlogin")==false && self::Get("Alogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
}

public static function CheckUserLogin()
{self::Start();
    if (self::Get("Mlogin")==false) 
    {
        return false;
    }
    else
    {
        return true;    
    }    
}

public static function Auto(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='user/login.php'>Login</a>";
 } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::Stop();
    }
 }

}

 public static function AutoA(){
    self::Start();
    if(self::Get("Mlogin")==false){
       echo "<a style=\"color:white;\" href='login.php'>Login</a>";
    } else {
       echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
      if(isset($_GET['action']) && ($_GET['action']== "logout")){
        self::StopB();
       }
   }

  }


  }

  ?>

This is My confirm.php code:

  <?php
   require_once("/../dataAccessLayer/dalSession.php");
   require_once("/../dataAccessLayer/dalLogin.php");
   session::check();
   ?>

   <?php
    if(isset($_GET['action']) && ($_GET['action']== "logout")){
        Session::Stop();
    }
    ?>
    <!doctype html>
    <html lang="en-US">
    <head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Success Notification Boxes</title>

   <link rel="stylesheet" type="text/css" media="all" href="style.css">
   <script type="text/javascript" src="https://ajax.googleapis.com          /ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   </head>

  <body>

  <div id="w">
  <div id="content">
  <!-- Icons source http://dribbble.com/shots/913555-Flat-Web-Elements -->
  <div class="notify successbox">
    <h1>Success!</h1>
    <span class="alerticon"><img src="" alt="checkmark" /></span>
    <p>Thanks For Your Purchase.</p>
    <p>Your Purchase Will Deliver Within 72 Hours.</p>
    <p>Any Question? Please Call: <b>+8801721557233</b></p>
    <p></p>
    </div>

    <h1><a class="navbar-brand" href="../index.php">Home</a></h1>


    </div><!-- @end #content -->
    </div><!-- @end #w -->

    </body>
    </html>
  • I think you can remove line 6, because a session already started before the checkout. – Maikel Jul 10 '17 at 14:44
  • Use session_status() to check ...and dont start a session if its already been started. Always use this simplest solution – geekzeus Jul 10 '17 at 14:47

2 Answers2

0

You can modify your function Start. Replace:

public static function Start(){
    session_start();
}

By:

public static function Start(){
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
}

It will fix your problem (the notice will not be displayed anymore). The idea is: if your session is already started, don't start it again.

Then if it does not work, remove ALL the space in your confirm.php file before the first <?php (it seems you have two spaces before the first <?php but I'm not sure it's because you copy/pasted it).

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
0

Remove spaces before

That characters are starting a session. and may be its better to use some template engine to generate html? Smarty, twig or simple php templates.