View previous topic :: View next topic |
Author |
Message |
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 2:36 am Post subject: Is user accessing page via https or http |
|
|
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 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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jan 20, 2005 9:26 am Post subject: |
|
|
$_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 |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 3:31 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jan 20, 2005 3:44 pm Post subject: |
|
|
enable $smarty->debugging and check if the variable is assigned correctly in the first place. |
|
Back to top |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 4:10 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 20, 2005 4:19 pm Post subject: |
|
|
The variables are in the debug console too, be sure caching is off. |
|
Back to top |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 4:30 pm Post subject: |
|
|
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 |
|
jaey Smarty Regular
Joined: 25 May 2004 Posts: 36 Location: Louisville, Kentucky, USA
|
Posted: Thu Jan 20, 2005 4:57 pm Post subject: |
|
|
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 |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 6:04 pm Post subject: |
|
|
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 |
|
jaey Smarty Regular
Joined: 25 May 2004 Posts: 36 Location: Louisville, Kentucky, USA
|
Posted: Thu Jan 20, 2005 6:15 pm Post subject: |
|
|
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 |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 6:22 pm Post subject: |
|
|
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 |
|
jaey Smarty Regular
Joined: 25 May 2004 Posts: 36 Location: Louisville, Kentucky, USA
|
Posted: Thu Jan 20, 2005 6:27 pm Post subject: |
|
|
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 |
|
christophkl Smarty Rookie
Joined: 20 Jan 2005 Posts: 8
|
Posted: Thu Jan 20, 2005 6:34 pm Post subject: |
|
|
WOW! 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 |
|
emmanouel Smarty Rookie
Joined: 02 Nov 2017 Posts: 7
|
Posted: Thu Mar 01, 2018 9:36 pm Post subject: similiar problem |
|
|
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... |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Mar 01, 2018 11:10 pm Post subject: |
|
|
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 |
|
|