PHP example for page using session login

Andy Simpson Posted in General Discussion 3 years ago

I wanted to host an external PHP page in my OSSN document root, linked to from OSSN, but also authenticated using the OSSN session data. I needed to be able to do things not available using custom pages.

Here is how I did it (working source).

Put this at the top of your external page. If they are logged into OSSN, they will be able to access it. If they are not logged into OSSN, they will be returned to your OSSN login page.

I would not rely on this working through future upgrades or new versions though.

<?php

session_start();

if (empty($SESSION["OSSNUSER"])) {
header('Location: http://www.your_ossn_url.com/login');
}

$sessiondata = $SESSION;

$sessiondata = jsondecode(jsonencode($sessiondata), true);

$guid = $sessiondata['OSSNUSER']['guid'];
$firstname = $sessiondata['OSSNUSER']['firstname'];
$lastname = $sessiondata['OSSNUSER']['lastname'];

?>

Replies
German Michael Zülsdorff Replied 3 years ago

Actually, there is no other option.
But I'm afraid you got me wrong anyway: I tried asking you to have a look into the code of ossn_com.php to get an idea which way to develop a standalone page with its own header.
Nevertheless there's no reason for not using this component. You should see links like
enter image description here
in your left menu. And clicking any of them will forward you to that standalone page I'm talking about.

au Andy Simpson Replied 3 years ago

@~Z~Man I tried your System-Info component. I got to the page that asks you to enter your email address that you used here, which I did, then clicked Save. What do I do from here? Can't see any other options. Do you have a link to documentation? Cheers

au Andy Simpson Replied 3 years ago

I think what it came down to for me was, I already had this big long complex PHP and Javascript page built outside of OSSN, and all I needed was for it to be authenticated via OSSN session. And to convert it into OSSN custom page, was harder than the code I used here. In other words lazy lol.

au Andy Simpson Replied 3 years ago

Thank you I will check it out. I find it a bit hard to find the info for various bits and pieces, but i'm getting there! Cheers

German Michael Zülsdorff Replied 3 years ago

Hmm ... honestly I'm a little clueless ... especially regarding the PHP part. Isn't PHP not PHP throughout the system? :)

Anyway, give it a try and have a look into my System-Info component's com file.
I think it's basically doing what you want:
It comes with a standalone html skeleton on one hand, but you may use any of Ossn's core functions and classes. And you can code the pagehandler to allow only logged in member visits.

au Andy Simpson Replied 3 years ago

Screenshot of source, formatted correctly

https://imgur.com/JYMPkCj

au Andy Simpson Replied 3 years ago

~Z~Man thank you for all your work on this project. I have used many of your posts for reference while setting my OSSN up, and they have been extremely helpful.

au Andy Simpson Replied 3 years ago

This is URL of my hand crafted external PHP page: https://server01.goldhuntersvictoria.com.au/maps/

You will see, if you are not logged into my OSSN, you will be returned to my OSSN login page. Once you login, you can load the page fine. The page is Gold Map overlays using Google Maps API, for Victoria Australia.

au Andy Simpson Replied 3 years ago

@~Z~Man Using a custom page, I couldn't work out how to set my own elements inside the html HEAD section, eg links to JS

How to do header redirections etc

Couldn't work out how to set BODY tag in html to have an onload=js argument.

Also couldn't get my PHP working the way I wanted.

German Michael Zülsdorff Replied 3 years ago

Thanks for providing that code, Andy.
But why not simply using a custom page?