Only run php function if user is logged in?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
terrylb
Registered User
Posts: 11
Joined: Mon Sep 03, 2018 8:12 pm

Re: Only run php function if user is logged in?

Post by terrylb » Thu Sep 06, 2018 4:48 pm

Thank you for the followup Brf. The php and js was written by someone else way back in 2014 and has been working until moving to 3.2. I'm just trying to maintain the functionality. I appreciate your suggestion that the orthodox way to do this is via an extension. I was hoping this could be solved quickly, even if it's not the ideal way, versus what looks like a week or two to figure out how to write an extension.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3301
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Only run php function if user is logged in?

Post by kasimi » Thu Sep 06, 2018 4:58 pm

This should work.

PHP:

Code: Select all

$template->assign_var('AWS_URL', append_sid($phpbb_root_path . 'aws.' . $phpEx));
HTML:

Code: Select all

<script>
var awsUrl = '{{ AWS_URL }}';
</script>
JS:

Code: Select all

$.ajax({ url: awsUrl,

terrylb
Registered User
Posts: 11
Joined: Mon Sep 03, 2018 8:12 pm

Re: Only run php function if user is logged in?

Post by terrylb » Thu Sep 06, 2018 8:21 pm

Thank you kasimi, I'll give it a go.

BTW, your avatar cracks me up every time I look at it! :)

terrylb
Registered User
Posts: 11
Joined: Mon Sep 03, 2018 8:12 pm

Re: Only run php function if user is logged in?

Post by terrylb » Fri Sep 07, 2018 5:26 pm

Unfortunately that didn't work. Adding your suggestions results in the video src attribute being stuffed with the entire forum topic html. aws.php isn't actually getting called.

As a recap, on a forum page where we post video, the ajax call in aws.js gets the video tag and posts the video filename to aws.php, which then checks to see if the user is logged in, and if so generates an AWS signed url and returns it back to ajax which stuffs it into the video src attribute. Works great in 3.1.6 and earlier.

Per your suggestion this was added to the html, /styles/prosilver/templates/overall_header.html, above the script calls to include jquery and aws.js.

Code: Select all

<script>
var awsUrl = '{{ AWS_URL }}';
</script>
This replaced the existing line in aws.js where the url was hardcoded to "aws.php"

Code: Select all

$.ajax({ url: awsUrl,
and this was added above the session management stuff in aws.php

Code: Select all

$template->assign_var('AWS_URL', append_sid($phpbb_root_path . 'aws.' . $phpEx));
I'm sure it's something basic I'm missing. Any help greatly appreciated.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3301
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Only run php function if user is logged in?

Post by kasimi » Fri Sep 07, 2018 7:28 pm

terrylb wrote:
Fri Sep 07, 2018 5:26 pm
and this was added above the session management stuff in aws.php

Code: Select all

$template->assign_var('AWS_URL', append_sid($phpbb_root_path . 'aws.' . $phpEx));
The AWS_URL template variable needs to be assigned in a file that renders the page you want the AWS script to be called on, not aws.php itself. Since you've edited overall_header.html, I assume you want aws.js included on every page? You could add the template variable here. However, to not waste resources, it's recommended to only include it when actually needed.
terrylb wrote:
Thu Sep 06, 2018 8:21 pm
BTW, your avatar cracks me up every time I look at it! :)
:D

terrylb
Registered User
Posts: 11
Joined: Mon Sep 03, 2018 8:12 pm

Re: Only run php function if user is logged in?

Post by terrylb » Fri Sep 07, 2018 9:31 pm

I had a strong suspicion the template call wasn't in the right place. Thank you very much kasimi, that fixed it. I agree, it should really only be included on forum posts since that's where videos live, but it's a private, very lightly used board, at most 3 simultaneous users, so I don't think the wasted resources will be a big deal. If I did change my mind, would a better place be template/posting_body.html?

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3301
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Only run php function if user is logged in?

Post by kasimi » Sat Sep 08, 2018 11:28 am

Assigning the AWS_URL template variable on every page doesn't cost much. As long as you include the aws.js script only on pages where you need it it's fine. You probably want to include it in posting_body.html, viewtopic_body.html and ucp_pm_viewmessage.html.

Post Reply

Return to “phpBB Custom Coding”

Who is online

Users browsing this forum: No registered users and 7 guests