Seite 1 von 1

Zugriff von cSession auf das PHP Session System

Verfasst: Mo 10. Feb 2014, 13:57
von mikesteiner99
Hallo Community
Hat schon mal jemand versucht aus einem Contenido Modul auf das Session System von PHP zuzugreifen? Wenn zum Beispiel in einem externen PHP Script mit einem Image Header (<img src="image.php"</img>) eine normale $_SESSION geschrieben wird habe ich keinen Zugriff aus Contenido cSession da drauf. Gibt es einen Workaround um dies zu bewerkstelligen?
Grüsse aus Züri

Re: Zugriff von cSession auf das PHP Session System

Verfasst: Mo 10. Feb 2014, 21:45
von xmurrix
Hallo mikesteiner99,

die Frontendsession von CONTENIDO hat einen eigenen Sessionnamen, du musst diesen Namen als "namespace" verwenden, um Sessiondaten zwischen CONTENIDO und deiner image.php auszutauschen.

Nehmen wir an, deine image.php liegt im Mandantenverzeichnis "cms", dann solltest du darin am Anfang CONTENIDO initialisieren, z. B. mit folgendem Code:

Code: Alles auswählen

<?php

$cmsDir = str_replace('\\', '/', realpath(dirname(__FILE__) . '/'));

if (!defined('CON_FRAMEWORK')) {
    define('CON_FRAMEWORK', true);
}

global $contenido_path, $cfg;

// Set path to current frontend
$frontend_path = $cmsDir . '/';

/*
 * Do not edit this value!
*
* If you want to set a different enviroment value please define it in your .htaccess file
* or in the server configuration.
*
* SetEnv CON_ENVIRONMENT development
*/
if (!defined('CON_ENVIRONMENT')) {
    if (getenv('CONTENIDO_ENVIRONMENT')) {
        $sEnvironment = getenv('CONTENIDO_ENVIRONMENT');
    } elseif (getenv('CON_ENVIRONMENT')) {
        $sEnvironment = getenv('CON_ENVIRONMENT');
    } else {
        // @TODO: provide a possibility to set the environment value via file
        $sEnvironment = 'production';
    }

    define('CON_ENVIRONMENT', $sEnvironment);
}

// Include the config file of the frontend to initialize client and language id
include_once($frontend_path . 'data/config/' . CON_ENVIRONMENT . '/config.php');

// CONTENIDO startup process
if (!is_file($contenido_path . 'includes/startup.php')) {
    die("<h1>Fatal Error</h1><br>Couldn't include CONTENIDO startup.");
}
include_once($contenido_path . 'includes/startup.php');

// Clients local configuration
if (file_exists($frontend_path . 'data/config/config.local.php')) {
    @include ($frontend_path . 'data/config/config.local.php');
}

// Frontend
cRegistry::bootstrap(array(
    'sess' => 'cFrontendSession',
));

// Check if session already started...
if (!session_id()) {
    session_start();
}


// Hier dein Code...

Gruß
xmurrix

Re: Zugriff von cSession auf das PHP Session System

Verfasst: Di 11. Feb 2014, 15:30
von mikesteiner99
Hallo xmurrix

Danke für Deine Antwort. Trotzdem steh ich auf dem Schlauch. Das CONTENIDO initialisieren ist mir bekannt. Nur werden bei Deinem Beispiel keine Sessions gespeichert. Verwende ich an Deinen Code angefügt folgende Zeilen:

Code: Alles auswählen

$myVar = 58;
if($sess->isRegistered('myVar')) {
  echo "myVar = " . $myVar . " ist bereits gespeichert.";
} else {
  $sess->register('myVar');
  echo "myVar = " . $myVar . " wurde in der Session gespeichert.";
}
erhalte ich nie die Antwort: wurde in der Session gespeichert. Erstelle ich ein Modul innerhalb von Contenido funktioniert dies einwandfrei.

Re: Zugriff von cSession auf das PHP Session System

Verfasst: Di 11. Feb 2014, 15:39
von mikesteiner99
Hoberla ... habs herausgefunden.
Starts the session and rebuilds the variables
LÖSUNG:

Code: Alles auswählen

$sess->start();
hat im externen Code am Ende gefehlt. Irgendwie logisch. :idea: :lol:

Re: Zugriff von cSession auf das PHP Session System

Verfasst: Sa 13. Sep 2014, 15:05
von mikesteiner99
Hallo xmurrix

Noch eine Frage hierzu: Weist Du grad aus dem Stehgreif, welche Zeile ergänzt werden müsste, um auch auf die Modul-Übersetzungen zugreifen zu können?

Es Grüsst, Mike
xmurrix hat geschrieben:Hallo mikesteiner99,

die Frontendsession von CONTENIDO hat einen eigenen Sessionnamen, du musst diesen Namen als "namespace" verwenden, um Sessiondaten zwischen CONTENIDO und deiner image.php auszutauschen.

Nehmen wir an, deine image.php liegt im Mandantenverzeichnis "cms", dann solltest du darin am Anfang CONTENIDO initialisieren, z. B. mit folgendem Code:

Code: Alles auswählen

<?php

$cmsDir = str_replace('\\', '/', realpath(dirname(__FILE__) . '/'));

if (!defined('CON_FRAMEWORK')) {
    define('CON_FRAMEWORK', true);
}

global $contenido_path, $cfg;

// Set path to current frontend
$frontend_path = $cmsDir . '/';

/*
 * Do not edit this value!
*
* If you want to set a different enviroment value please define it in your .htaccess file
* or in the server configuration.
*
* SetEnv CON_ENVIRONMENT development
*/
if (!defined('CON_ENVIRONMENT')) {
    if (getenv('CONTENIDO_ENVIRONMENT')) {
        $sEnvironment = getenv('CONTENIDO_ENVIRONMENT');
    } elseif (getenv('CON_ENVIRONMENT')) {
        $sEnvironment = getenv('CON_ENVIRONMENT');
    } else {
        // @TODO: provide a possibility to set the environment value via file
        $sEnvironment = 'production';
    }

    define('CON_ENVIRONMENT', $sEnvironment);
}

// Include the config file of the frontend to initialize client and language id
include_once($frontend_path . 'data/config/' . CON_ENVIRONMENT . '/config.php');

// CONTENIDO startup process
if (!is_file($contenido_path . 'includes/startup.php')) {
    die("<h1>Fatal Error</h1><br>Couldn't include CONTENIDO startup.");
}
include_once($contenido_path . 'includes/startup.php');

// Clients local configuration
if (file_exists($frontend_path . 'data/config/config.local.php')) {
    @include ($frontend_path . 'data/config/config.local.php');
}

// Frontend
cRegistry::bootstrap(array(
    'sess' => 'cFrontendSession',
));

// Check if session already started...
if (!session_id()) {
    session_start();
}


// Hier dein Code...

Gruß
xmurrix

Re: Zugriff von cSession auf das PHP Session System

Verfasst: So 18. Dez 2016, 15:22
von malsdgtac
Hallo,

ich stehe dazu leider auch auf dem Schlauch. Ich will eigentlich was ganz einfaches: in einem Modul will ich die Variable $_SESSION['merkliste'] befüllen und in einem anderem Modul auslesen. Funktioniert aber wie unter 4.8 leider nicht. Kann mir jemand ein Codebeispiel posten? Ich wäre euch voll dankbar.

Re: Zugriff von cSession auf das PHP Session System

Verfasst: So 18. Dez 2016, 23:42
von homtata
smac, bitte keine Doppelposts! Du hast zu dieser Frage bereits einen eigenen Thread eröffnet. Wenn du nicht weiterkommst, dann poste bitte dort, was du versuchst, und erste Anleitungsschritte sind dort bereits auch genannt.
LG

Re: Zugriff von cSession auf das PHP Session System

Verfasst: Mo 19. Dez 2016, 08:26
von malsdgtac
Sorry, ich poste meine Antwort dort.