Avatar of Irene Bontà
Irene Bontà
 asked on

Codeigniter sharing session with a simple php blog

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;

Open in new window


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;

Open in new window


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.
PHPCodeIgniter* cookie

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

What are the exact URLs of each of these sites?  I'm wondering if they can share cookies.
Irene Bontà

ASKER
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Irene Bontà

ASKER
Where I have to put this script? Thank you very much for your help!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ray Paseur

You can put it anywhere that it can be accessed via a URL - just choose a name, like temp_irene.php and store it on the server.  Then open a browser, go to the URL and see that it works (there will be some browser output, but not much).  Once you've done that on both domains, just post the link here.  I'll test as soon as I can, then you can delete the script.  Thanks!
Ray Paseur

abandoned question, accepting commonsense comments