Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Is user accessing page via https or http
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 2:36 am    Post subject: Is user accessing page via https or http Reply with quote

I have spent the past day trying to figure out how to include one chunk of JavaScript if the user is accessing the page via https and another if it is http. I can get it to work on a regular PHP page but I want to include it as a template because then I will only have to change the one template not every page. The template I am adding this two is nested two or three down from the pages eg. {includ} when I try to put {php} $_SERVER['HTTPS'] but it doesn't work Question I tried doing the $_SERVER['HTTPS'] in the main page and then assigning the result to a variable but I could not pull up the variable in the template.

If any one can help with any of these issues it would be much appreciated. I am new to smarty and php so try to keep it basic.

Thanks Alot.

Below are some of the scripts I have tried


{php}
if (!$_SERVER[HTTPS]){
echo "<\"secured javascript code\">";
} else {
echo "<\"unsecured javascript code\">";
}
{/php}

on main PHP page:

if ($_SERVER[HTTPS]){
$httpsornot = true;
} else {
$httpsornot = false;
}
$smarty->assign("httpsornot", $httpsornot);

and then in template:

{if $httpsornot}
{ include file="secure_js.tpl" }
{else}
{ include file="nsecure_js.tpl" }
{/if}
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jan 20, 2005 9:26 am    Post subject: Reply with quote

$_SERVER['HTTPS'] is not boolean, but a string containing the words "on" or "off".

[php:1:1c05d66f5a]$smarty->assign('https', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
[/php:1:1c05d66f5a]
Code:

{if $https}
...
{else}
...
{/if}

should do.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 3:31 pm    Post subject: Reply with quote

Hi Thanks for the correction. I still am having problems pulling up the variable in the template here is what I have. The main page home.php then included is auth.php with

require "./auth.php";

in auth.php I included your code
$smarty->assign('httpsornot', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');

then at the bottom of home.php is

func_display("customer/home.tpl",$smarty);

and within home.tpl is the code

{if $httpsornot}
{ include file="secure_js.tpl" }
{else}
{ include file="nsecure_js.tpl" }
{/if}
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jan 20, 2005 3:44 pm    Post subject: Reply with quote

enable $smarty->debugging and check if the variable is assigned correctly in the first place.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 4:10 pm    Post subject: Reply with quote

When I enabled debugging I just got a popup with included template and config files. How do I get the assigned variables?
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jan 20, 2005 4:19 pm    Post subject: Reply with quote

The variables are in the debug console too, be sure caching is off.
Back to top
View user's profile Send private message Visit poster's website
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 4:30 pm    Post subject: Reply with quote

All I can see are items ending in .tpl would that mean that their are no assigned variables. when I try to go directly to auth.php with the debuger enabled there is no popup.
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
jaey
Smarty Regular


Joined: 25 May 2004
Posts: 36
Location: Louisville, Kentucky, USA

PostPosted: Thu Jan 20, 2005 4:57 pm    Post subject: Reply with quote

Based on some of the code snippets you posted, it looks like you're trying to add code to an existing application named "X-Cart". I know that "X-Cart" has a specific list of global variables it allows and any variables not in that list are "unset". It appears that "X-Cart" allows $HTTP_SERVER_VARS['HTTPS'] or just $HTTPS but not $_SERVER['HTTPS']
Back to top
View user's profile Send private message
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 6:04 pm    Post subject: Reply with quote

I do know that $_SERVER['HTTPS'] works because if I do an echo of $_SERVER['HTTPS'] it comes up ON etc.
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
jaey
Smarty Regular


Joined: 25 May 2004
Posts: 36
Location: Louisville, Kentucky, USA

PostPosted: Thu Jan 20, 2005 6:15 pm    Post subject: Reply with quote

christophkl wrote:
I do know that $_SERVER['HTTPS'] works because if I do an echo of $_SERVER['HTTPS'] it comes up ON etc.


Is your "echo" statement before or after this line:

Code:
require "./auth.php";
Back to top
View user's profile Send private message
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 6:22 pm    Post subject: Reply with quote

It only works in auth.php itself so I think I see what you mean, probably as it goes on the next page it unsets it. Do you know anyway to get around this?
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
jaey
Smarty Regular


Joined: 25 May 2004
Posts: 36
Location: Louisville, Kentucky, USA

PostPosted: Thu Jan 20, 2005 6:27 pm    Post subject: Reply with quote

christophkl wrote:
It only works in auth.php itself so I think I see what you mean, probably as it goes on the next page it unsets it. Do you know anyway to get around this?


Sure, as I said in an earlier post, use $HTTP_SERVER_VARS['HTTPS'] or $HTTPS instead of $_SERVER['HTTPS'].
Back to top
View user's profile Send private message
christophkl
Smarty Rookie


Joined: 20 Jan 2005
Posts: 8

PostPosted: Thu Jan 20, 2005 6:34 pm    Post subject: Reply with quote

WOW! Very Happy You don't know how long I have spent on this problem! Next time I will mention X-Cart to start with.

Thanks to everyone!
_________________
{if $allelse eq " "}
{read "manual"}
Back to top
View user's profile Send private message
emmanouel
Smarty Rookie


Joined: 02 Nov 2017
Posts: 7

PostPosted: Thu Mar 01, 2018 9:36 pm    Post subject: similiar problem Reply with quote

Hello
I have problem with mixed content in some links , active and passive, so the ssl protection is not working in the browser. How I could redirect http requests to https?

Somebody told me to find(..!) solution in this tread,but I am not expert... Rolling Eyes
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu Mar 01, 2018 11:10 pm    Post subject: Reply with quote

With a problem of having page resources of http but the page is from https, the solution is to not use http or https.

Use simply:
//www.example.com/image.png

The browser will use the same protocol as was used to fetch the page.
Code:
{$SITE_URL|regex_replace:"https?//":"//"}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP