We help IT Professionals succeed at work.

Codeigniter sharing session with a simple php blog

Irene Bontà
Irene Bontà asked
on
1,106 Views
Last Modified: 2017-06-10
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.
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
What are the exact URLs of each of these sites?  I'm wondering if they can share cookies.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Where I have to put this script? Thank you very much for your help!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
abandoned question, accepting commonsense comments

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions