This question already has answers here:
PHP sessions that have already been started [duplicate]
(11 answers)
Closed 5 years ago.
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>
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).
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.
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Hello for an easy data handling I though of creating a class but I think I must have missed something as it stops the page from loading when I include it.
I think that the error must be something very simple but I don't have much experience in coding in php so if someone could point me in the direction of the mistake and give me an idea of what to implement to resolve this issue.
Like said above in this scenario it will ouput the page until test and then it wont put the rest.
index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>test</p>
<?php include("class_lib.php"); ?>
<p>test2</p>
</body>
</html>
class_lib.php:
<?php
class MyAbility{
var $Name;
var $URL;
var $Level1;
var $Level2;
var $Level2;
var $Level4;
var $Level5;
function set_Name($new_name) {
$this->Name = $new_name;
}
function get_Name() {
return $this->Name;
}
function set_URL($new_URL) {
$this->URL = $new_URL;
}
function get_URL() {
return $this->URL;
}
function set_Level1($new_Level1) {
$this->Level1 = $new_Level1;
}
function get_Level1() {
return $this->Level1;
}
function set_Level2($new_Level2) {
$this->Level2 = $new_Level2;
}
function get_Level2() {
return $this->Level2;
}
function set_Level3($new_Level3) {
$this->Level3 = $new_Level3;
}
function get_Level3() {
return $this->Level3;
}
function set_Level4($new_Level4) {
$this->Level4 = $new_Level4;
}
function get_Level4() {
return $this->Level4;
}
function set_Level5($new_Level5) {
$this->Level5 = $new_Level5;
}
function get_Level5() {
return $this->Level5;
}
}
?>
Declared twice my friend :-)
var $Level2;
https://phpcodechecker.com/
More about forcing to print out the error.
http://www.ronaldpringadi.com/archives/introduction-to-logging-and-tracing-in-php/
I have a session class as follows:
<?php
class Session {
function __construct() {
if (empty(session_id())) {
session_start();
}
}
function AddMessage($msg="") {
if (!is_array($_SESSION['messages'])) {
$_SESSION['messages'] = [];
}
array_push($_SESSION['messages'], $msg);
}
function GetMessages() {
$messages = $_SESSION['messages'];
unset($_SESSION['messages']);
return $messages;
}
}
?>
Also there is a php file in my layouts directory:
<?php if (!empty($_SESSION['messages'])) { ?>
<div class="messages">
<ul>
<?php
$messages = $session->GetMessages();
foreach ($messages as $message) {
echo "<li class=\"message\">{$message}</li>";
}
?>
</ul>
</div>
<?php }?>
This piece of code above is included at the top of my pages.
The problem is that in case of single-page submission handling -done in the middle of the page - the messages are printed out before I set it with the AddMessage() method.
Is there a simple way to get around this issue, or I have to rethink my code flow?
probably a newb question here, but how do I return a CI anchor() call from within a function. I want to "hide" a button if a variable is set to a certain value.
CI's URL helper documentation: https://www.codeigniter.com/user_guide/general/helpers.html
A pseudo example that won't work (can't return the URL helper anchor('',''):
$prevAvailCompID = 0;
function hideButton($prevCompID)
{
if($prevCompID == 0)
{
return anchor('/getcomps/getSpecificComp/'.$prevCompID , 'PREV COMP');
//I've also tried return echo anchor(...)
}
}
further down on the page:
<div id="prevBtnContainer"><? hideButton($prevAvailCompID); ?></div>
You don't need to return the anchor() function. You can just use as this
UPDATED CODE
public function test(){
?>
<h1>test H1</h1>
<div id="prevBtnContainer"><?php $this->hideButton(0); ?></div>
<div id="1prevBtnContainer"><?php $this->hideButton(1); ?></div>
<?php
}
private function hideButton($prevCompID)
{
if($prevCompID == 0)
{
echo anchor('/getcomps/getSpecificComp/'.$prevCompID , 'PREV COMP');
}
}
I've tested this in my CodeIgniter and its working.
When I enter this code it makes the page unusable. I have tried variations and yet it still doesn't work.
I am trying to make it display "Login" When you are not logged in and to display the username when logged in.
<?php
if(!$fgmembersite->CheckLogin())
{
return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
} else {
echo "Login"
}
?>
If more is necessary please comment.
Section this code is in
<li class='active'><a>
<?php
if(!$fgmembersite->CheckLogin())
{
isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
} else {
echo "Login";
}
?>
</a>
And these are the functions that initiate the code
function CheckLogin()
{
if(!isset($_SESSION)){ session_start(); }
$sessionvar = $this->GetLoginSessionVar();
if(empty($_SESSION[$sessionvar]))
{
return false;
}
return true;
}
function UserFullName()
{
return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
}
You are missing a semi-colon on line 7, should be:
echo "Login";
Here is my code:
My root directory is: root
An index.php file located at the root/index.php
<?php
require_once('root/includes/initialize.php');
<?php template('header.php', 'TITLE');?>;
?>
<div id="main">
//SOME CONTENT
</div>
My initialize.php file gets all my core include files and puts them into one "require_once". Located in root/includes/initialize.php
<?php
//Define Path
defined('LIB_PATH') ? null : define('LIB_PATH', 'root/includes/');
//Core Functions
require_once(LIB_PATH.'functions.php');
//Core Objects
require_once(LIB_PATH.'_database.php');
require_once(LIB_PATH.'_session.php');
//Classes
require_once(LIB_PATH.'_user.php');
?>
updated**
My functions.php file includes a simple templating function that grabs a template file such as my header.php. It is located in root/includes/functions.php
<?php
//Templating
function template($path="", $pageTitle=NULL) {
if ($pageTitle != NULL) {
$_POST['page_title'] = $pageTitle;
}
include(root/public/templates/'.$path);
}
?>
My _session.php file takes care of my session control. Located in root/includes/_session.php
<?php
/**
* Class for Sessions
*/
class Session
{
public $logged_in = FALSE;
public $uid;
function __construct() {
session_start();
$this->check_login();
}
public function check_login() {
if (isset($_SESSION['uid'])) {
$this->uid = $_SESSION['uid'];
$this->logged_in = TRUE;
} else {
unset($this->uid);
$this->logged_in = FALSE;
}
}
public function logged_in() {
return $this->logged_in;
}
public function login($user) {
if ($user) {
$this->uid = $_SESSION['uid'] = $user;
$this->logged_in = TRUE;
}
}
public function logout() {
unset($_SESSION['uid']);
session_unset();
session_destroy();
redirect(WEB_ROOT);
}
}
$session = new Session();
?>
updated**
My header.php holds the top of all the pages in my site. Located in root/public/templates/header.php. This is the file I'm having trouble with, I cant figure out why I am unable to echo out the $session->uid or the $_SESSION['uid'] in this file.
<html>
<head>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="root/public/css/style.css">
<title>MY SITE</title>
</head>
<body>
<div id="header">
<div id="logo">
<?php echo $_POST['page_title'];?>
</div>
<?php echo $session->uid;?> //DOESN'T WORK
</div>
I am able to echo out everything just fine in my index.php file and the other files on my site, but not in the included header.php. Any one know why? Thanks.
session_start() must be called at the start of EVERY php file that is going to either set or get a session variable. The only place I see you calling session_start() is in the one file.
http://www.php.net/manual/en/session.examples.basic.php
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
Also on a side note. I'm looking at your class Session and I'm not seeing any $mySession = new Session(); anywhere to also start a session.
UPDATE:
I recreated your basic file structure and code in my IDE and got it work by adding this line in the class.
public function check_login() {
if (isset($_SESSION['uid'])) {
$this->uid = $_SESSION['uid'];
$this->logged_in = TRUE;
}
else {
unset($this->uid);
$this->logged_in = FALSE;
$_SESSION['uid'] = session_id();
/*Add this next line */
$this->uid = $_SESSION['uid'];
}
}
The first time I ran index.php just the <?php echo $_SESSION['uid']; ?> part of header worked. Refreshed and <?php echo $session->uid; ?> also worked so it echoed twice. This tells me your class isn't assigning the ID to a class variable, hopefully this is the desired out come as it worked on my end, or you can tweek it as needed.
UPDATE 2:
Function File (edit to match your paths but you need to return a string)
<?php
//Templating
function template($path = "", $pageTitle = NULL) {
if ($pageTitle != NULL) {
$_POST['page_title'] = $pageTitle;
}
return "$path";
}
?>
Then in the Index.php file add this way instead:
<?php
require_once('initialize.php');
include(template('header.php', 'TITLE'));
//include('header.php');
?>
<div id="main">
//SOME CONTENT
</div>
</body>
</html>
_session.php file:
<?php
/**
* Class for Sessions
*/
class Session
{
public $logged_in = FALSE;
public $uid;
function __construct() {
session_start();
$this->check_login();
}
public function check_login() {
if (isset($_SESSION['uid'])) {
$this->uid = $_SESSION['uid'];
$this->logged_in = TRUE;
}
else {
unset($this->uid);
$this->logged_in = FALSE;
$_SESSION['uid'] = session_id();
$this->uid = $_SESSION['uid'];
}
}
public function logged_in() {
return $this->logged_in;
}
public function login($user) {
if ($user) {
$this->uid = $_SESSION['uid'] = $user;
$this->logged_in = TRUE;
}
}
public function logout() {
unset($_SESSION['uid']);
session_unset();
session_destroy();
redirect(WEB_ROOT);
}
}
$session = new Session();
?>
And header.php
<html>
<head>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="style.css">
<title>MY SITE</title>
</head>
<body>
<div id="header">
<div id="logo">
<?php echo $_POST['page_title'];?>
</div>
<?php echo $session->uid; ?> //WORKS NOW
<?php echo $_SESSION['uid']; ?> //WORKS NOW
</div>
The question is pretty vague. My guess would be, that since your index.php file in located in root/index.php, then your include paths:
require_once('root/includes/initialize.php');
include('root/public/templates/header.php');
are incorrect. You don't start with a /, so paths are relative, and considering the location of your index.php, you are including root/root/includes/initialize.php. If that's the case, you should easily spot that by the lack of <title>MY SITE</title> and TITLE on your page. Haven't you?
If that's the problem, I suggest you define some kind of HOME constant, for example
define ('HOME', dirname(__FILE__));
// or define ('HOME', __DIR__); depending on your PHP version
so that you can then include everything relative to that constant
require_once(HOME . '/includes/initialize.php');
Other than that I don't see any errors in your code.