troubleshooting Question

Codeigniter sharing session with a simple php blog

Avatar of Irene Bontà
Irene Bontà asked on
PHPCodeIgniter* cookie
6 Comments1 Solution1128 ViewsLast Modified:
I have to solve this issue which I'm not able to solve :( I have a blog in PHP and an ecommerce site built with CodeIgniter. Both sites have the same session informations shared, but if I go on the blog and I login, when I pass on the ecommerce the user is not recognized and the ecommerce ask for a new login to the user.

In the blog part I set the session in this way:

<?php

include("config.php");

if(isset($_GET['logout'])){
    $servername = 'localhost';
    $username = 'wtgecomm_ecomm';
    $password = 'ElFDeOD$o_qd';
    $dbname ='wtgecomm_ecommerce';
    $conn = new mysqli($servername, $username, $password, $dbname);
     $sql = "DELETE FROM `ci_sessions` WHERE id="."'".$_COOKIE['ci_session']."'";
    if (!$conn->query($sql)) {
   // printf("Message d'erreur : %s\n", $conn->error);
}
    //$result = $conn->query($sql);
    //var_dump($result);
$_SESSION['login_id'] = '';
$_SESSION['customer_id'] = '';
unset($_SESSION['customer_id']);
unset($_SESSION['client']);
unset($_COOKIE['ci_session']);
session_destroy();
//var_dump($_SESSION);
header( 'Location: /'); 
}
if(isset($_REQUEST['login_id']) && !empty($_REQUEST['login_id']) ){

    $ci_session = $_COOKIE['ci_sessions'];
    $login_id = $_REQUEST['login_id'];
//  $sql = "INSERT INTO `ci_sessions` (`id`,`data`) VALUES ('2121212','121212121212')";
//  $ctx->exec($sql) ;
    $servername = 'localhost';
    $username = 'wtgecomm_ecomm';
    $password = 'ElFDeOD$o_qd';
    $dbname ='wtgecomm_ecommerce';
    $conn = new mysqli($servername, $username, $password, $dbname);
     $query = "SELECT * FROM `users` WHERE id="."'".$login_id."'";
    if ($result = $conn->query($query)) {

    /* Récupère un tableau associatif */
    while ($row = $result->fetch_assoc()) {

        $cust_email=$row['username'];
        $cust_pass=$row['password'];
    }

    /* Libération des résultats */
    $result->free();
}
$_SESSION['login_id']= $login_id;
$_SESSION['customer_id']=$login_id;
$_SESSION['cust_email']=$cust_email;

$_SESSION['userdata'] = array('user_id' => $login_id, 'username' =>$cust_email,'status'=>1,'password'=>$cust_pass );
$user=$_SESSION['userdata'];
echo true;
exit;
}
else {
    echo false;
    exit;
}
exit;

In the ecommerce config.php file (CodeIgniter), I have this session configuration:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;





//$config['sess_table_name'] = 'ci_sessions';
//$config['sess_match_useragent'] = TRUE;
//$config['sess_use_database'] = TRUE;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix'   = Set a cookie name prefix if you need to avoid collisions
| 'cookie_domain'   = Set to .your-domain.com for site-wide cookies
| 'cookie_path'     = Typically will be a forward slash
| 'cookie_secure'   = Cookie will only be set if a secure HTTPS connection exists.
| 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript)
|
| Note: These settings (with the exception of 'cookie_prefix' and
|       'cookie_httponly') will also affect sessions.
|
*/
$config['cookie_prefix']    = '';
$config['cookie_domain']    = '.wtgecommerce.powersoftware.in';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

Where I'm wrong? I tryed in many ways, to set up the same configuration for the user session but without solution... thank you for the help.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros