Jump to content

How to read PHP $_SESSION from PW when bootstrap is not an option


Moebius
 Share

Recommended Posts

I have situation like this.

Page 1:
https/www.domain.com/some_non_processwire_page.php
This page is out of my scope and it set some PHP session, e.g. $_SESSION['something']
There is no way that I can bootstrap PW in this page, I must leave $_SESSION['something'] as it is, so there is no option to bootstrap & and convert it to $session->something

Page 2:
https/www.domain.com/some-pw-page
This is normal PW templated page, full under my control.

I need to read $_SESSION['something'] from there.
I know PW has it's own $session object, but as I staed beofre, I can't bootstap PW from some_non_processwire_page.php and then do stuff only with PW $session

Is there any possible way that I can read $_SESSION['something'] from https/www.domain.com/some-pw-page

I found some topics about session here on forum, but none of this answers is solution for my situation.
 

Link to comment
Share on other sites

Dobro vece,

You can try this in your template or _init.php:

$cookieFile = $_SERVER['DOCUMENT_ROOT'] . "/pw-boilerplate/site/assets/cookies/cookies.txt"; // adjust this
if(!file_exists($cookieFile)) {
	$fh = fopen($cookieFile, "w");
	fwrite($fh, "");
	fclose($fh);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/pw-boilerplate/session.php'); // more about this below
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // Cookie aware
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // Cookie aware
$result = curl_exec($ch);
curl_close($ch);

$result will now show the value of your outside session.

pw-root/session.php does really only this:

<?php
session_start();
if(isset($_SESSION["outsidepw"])) {
	echo $_SESSION["outsidepw"];
}

This means that if your non-PW site creates a session named "outsidepw", you just output it to the browser, in a page that sits in the PW root (not inside site/templates). Since such PHP files at root-level know nothing about PW, you can do anything you want (they're not bootstrapping PW).

CURL will mimick a browser request, and that way you can get the external session var back into PW.

Just tested it out quickly. Instead of a full-fledged PHP app in root, I just created a PHP file that instantiates a session and declares a session value. But the main logic should still work, as long as you know the external session var...

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...