Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

How to? Include conditional content on Static page

Quote Reply
How to? Include conditional content on Static page
Hi,

I want to display certain types of content if the user is logged in & other content if the user is not logged in.

However, I want to do this while doing a 'static build' for the website.

I am trying below method & it does not seem to work completely.

1. page extension is .php
note: I am able to use other php codes and tags on this page.

2. Then I include below code at desired location on page:
<?php include "https://domain.com/a/cgi/page.cgi?p=dynamic-test"; ?>


Inside dynamic.html I have the following code:

Code:
<%if Usernamer%>
Hello <%Username%> : How are you ?
<%else%> User is not Logged in.
<%endif%>

However it does not work.

I thought that every time the page is viewed (called by the browser), it would trigger php include function & content of dynamic.html page would be displayed based on the condition if the user is logged in or not.

Any idea on how to make this work using php include function?

------------------------------------------

Btw.. I have tested above using iframe & it works as desired, however, I am trying to avoid iframe & use PHP.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to? Include conditional content on Static page In reply to
*Update*

When I use below php include method:

<?php
// file_get_contents example
$file = file_get_contents("https://domain.com/a/cgi/page.cgi?p=dynamic-test");
echo $file;
?>

It works, however it always shows 'User is not Logged in. ' even when user is logged in.

At first I thought it may be possible that it is using first condition regardless, so I changed the content of dynamic.html to below:

<%unless Username%>
user should login
<%else%>
Hello <%Username%>
<%endunless%>

However, for this it always outputs: 'user should login'.

Also, when I change the text for not logged in condition.. it displayed updated text.

However, even when the user is logged in, it only shows 'not logged in' condition text.

Any idea what is going on?

I understand PHP is server side, however I was hoping that when it fetches the full url, it will display content based in the full URL for according to the logged in user, since content would be different.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to? Include conditional content on Static page In reply to
The more I think about it, don't think it is possible via php.. :( ... (would love it if there was a way though)...

For now I am using below javascript for this purpose:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function(){
$("#includedContent").load("https://domain.com/a/cgi/page.cgi?p=dynamic");
});
</script>
<div id="includedContent"></div>

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to? Include conditional content on Static page In reply to
As far as I see and understand your task should work with php as long as your static pages have a php extension and are php.

Whatever you do with template code:

Code:
<%code%>

will be created upon build and therefore will not depend on any user logged in or not upon request.
If you build .php pages you could put anything inside them with dynamic conditons.

Javascript could work for some tasks but the more complex it gets the more difficult it would probably get.

I use LinksSQL in dynamic mode for all tasks with rewrite to make it appear static even though I might have some speed advantages from static pages.

Regards

n||i||k||o
Quote Reply
Re: [el noe] How to? Include conditional content on Static page In reply to
Yeah PHP code is working just fine, however I am unable to use if/elseif conditions in it for logged in users.

I have considered using dynamic mode instead of static build, as there are clear benefits of it, especially not having to do manually remove/delete the links. However, I still like the added benefits of having static pages. But yeah, in future, I do plan to consider using dynamic mode.

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to? Include conditional content on Static page In reply to
Not sure about php include with an URL but could it be that the login process is happening on your webserver "browser" and not your local.
That means you are kind of logged in but not really.
But as I am really not good with PHP this is just a guess.
What happens if you call https://domain.com/a/cgi/page.cgi?p=dynamic-test direcly from your browser?
Do you check the session within your PHP script?

Regards

n||i||k||o

Last edited by:

el noe: Mar 7, 2021, 8:27 AM
Quote Reply
Re: [el noe] How to? Include conditional content on Static page In reply to
Yes, I checked https://domain.com/a/cgi/page.cgi?p=dynamic-test via browser & it works as expected.

However, the reason for this seems to be what you expected. As PHP is a server-side script, it seems to be including the url/page content even before serving the page & as such not getting if/elseif condition based on user cookies.

Btw.. I can still use php & include page (https://domain.com/a/cgi/page.cgi?p=dynamic-test) for inputting other dynamic info on the page (as long as it is not cookie based)

However, since javascript is client-side, I am able to use include in it, but when using javascript, there is a slight delay, which is visible. (& iframe could be a second option, which also works, but same issue of slight-delay.)

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] How to? Include conditional content on Static page In reply to
I think you should reconsider your whole approach for it gets really divided up.
Nevertheless I think it could work if you pass the session with your request.

I started to play with PHP and LinksSQL and as always Andy added some helpful comments but I lost track on this idea:
https://www.gossamer-threads.com/...%3F_P310458/#p310458

Now you have the Username and could include your URL based on the condition if it is there or you could try to pass the session
Code:
$("#includedContent").load("https://domain.com/a/cgi/page.cgi?p=dynamic&s=$_COOKIE['s']");

As I work cookie based I am not sure if you could use URL and Cookie parameters at the same time.

Without seeing what you try to do my guess would be that you should get the Information in PHP and display whatever you want directly in PHP without including an URL.

Hope that helps you getting a little further.

Regards

n||i||k||o

Last edited by:

el noe: Mar 8, 2021, 12:12 AM
Quote Reply
Re: [el noe] How to? Include conditional content on Static page In reply to
Yeah something like that would work. As you said, you can't do conditional stuff based on user tags in static pages. So the easiest way, is just to make a new template like get_user_info.html , with:

Code:
<%if user.Username%>you are logged in<%else%>not logged in<%endif%>

Then use JS (or jQuery in this example), to grab that page and put it in the section you want:

Code:
$("#divIdForWhereItShouldGo").load("https://domain.com/a/cgi/page.cgi?p=get_user_info");

This should then give back the value dynamically, and put it into the page :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [el noe] How to? Include conditional content on Static page In reply to
Thanks.. For now I have started using javascript, and since the fetched content is visible only after scrolling down, it shouldn't work fine :)

Vishal
-------------------------------------------------------
Quote Reply
Re: [Andy] How to? Include conditional content on Static page In reply to
Thanks Andy,

I am already using above mentioned javascript method, however will also try this jquery method, since I am already loading jquery script.

SmileSmile

Vishal
-------------------------------------------------------