I have two landing pages (homepage1 and homepage2). If I land on homepage1, the logo link needs to change to homepage1 and keep it as I go to other pages. The same goes when I land on homepage2. I tried -
if (strstr($_SERVER['HTTP_REFERER'], 'homepage1.php') !== false) {
<a href='homepage1.php'><img src='logo.jpg'></a>
}
elseif (strstr($_SERVER['HTTP_REFERER'], 'homepage2.php') !== false ) {
<a href='homepage2.php'><img src='logo.jpg'></a>
}
It works when I go to one page but anymore than one the url and logo are gone. In other words, it doesn't hold on to the url.
I need it to hold on to the url based on what landing page I land on. And it needs to hold on to the url, no matter how many pages I go to.
Is this possible?
As #DragonYen pointed out , you need to use session variable as it can be used to persist state information between page requests.
session_start();
$ref = $_SERVER['HTTP_REFERER'];
$page = explode("/", $ref);
if($page[3] == "homepage1.php") {
$_SESSION['home'] = 1;
}
else if($page[3] == "homepage2.php") {
$_SESSION['home'] = 2;
}
Now you can check for session variable home
if ($_SESSION['home'] == 1) {
<a href='homepage1.php'><img src='logo.jpg'></a>
}
elseif ($_SESSION['home'] == 2) {
<a href='homepage2.php'><img src='logo.jpg'></a>
}
put this when you no longer need the session
unset($_SESSION['home'];
session_destroy();
Related
In OpenCart 2, I am editing the appearance/php of the header only in the "success"/"thank you" page (catalog/view/theme/*/template/common/success.tpl).
So, in catalog/view/theme/*/template/common/header.tpl I want to do something like:
if( $is_thank_you_page ){
echo "stuff";
// bonus: I wanted to get the order email but maybe it should be a different post
}
But how can I check in the header.tpl if it is the "success"/"thank you" page?
I tried setting a variable in success.tpl before printing the header with no results.
You could try something like this (go about it based on your URL):
<?php
$parameters = explode('/', $_SERVER['REQUEST_URI']);
if(end($parameters) === 'success.tpl'){
//the condition with $parameters depends on the exact look of your URL
//you could also access an index directly
}
Basically, it takes the REQUEST_URI (part after the domain), splits it around the / symbols and then checks if it ends with success.tpl
You could also make a switch for the end($parameters) instead of the if.
I don't know opencart structure, but if this value never change you can try with strpos/stripos, something like:
if(stripos($var_with_page_title, 'thank you') !== false) {
do_something();
}
If you want you detect checkout/success page in you header, do following:
open catalog/controller/common/header.php
find
// Menu
$this->load->model('catalog/category');
Add before
// success page checking
$data['success'] = '';
if (isset($this->request->get['route']) && $this->request->get['route'] == 'checkout/success') {
$data['success'] = true;
}
// looking for email from the order
$data['success_email'] = '';
if ($this->customer->isLogged()) {
$data['success_email'] = $customer_info['email'];
} elseif (isset(this->session->data['guest']['email'])) {
$data['success_email'] = $this->session->data['guest']['email'];
}
Now in catalog/view/theme/YOUR_THEME/template/common/header.tpl
add anywhere you like
<?php if ($success) { ?>
//do something
<?php if ($success_email) { ?><?php echo $success_email; ?><?php } ?>
<?php } ?>
With bonus email
I want to communicate with the session on two pages that I dynamically create on a Wordpress theme. I do this on a common header.php file, but I can't get the session value I sent from one page on the other. Could you help?
$pageName = $_SERVER['REQUEST_URI'];
if($pageName == "/test/"){
$_SESSION["1cKullaniciLogin"] = "TEST DENEME";
header("Location: /faaliyet-raporlarimiz/");
exit();
}
if($pageName == "/faaliyet-raporlarimiz/"){
if(ISSET($_SESSION["1cKullaniciLogin"]))
{
}
else
{
header("Location: /test/");
}
}
I've placed a hit counter on my page. It reads a text file, increments the number in the file, and later in the page, I output the incremented value.
$hitsFile = "hits/exps/stats.txt";
$hits = file($hitsFile);
$hits[0]++;
$fp = fopen($hitsFile , "w");
flock($fh, LOCK_EX);
fwrite($fp , $hits[0]);
fclose($fp);
My problem is that if I reload the page, the code will increment the hits. I don't want that. I thought of using session to fix that, but with session, in order the increment the hits again, I need to exit the site and visit again. I don't want that either.
I want it to increment not when I reload the page but when I revisit the page.
For example, let's say I have two-page website, Home and Contact, and on contact page I have a hit counter. I don't want the hit counter to increment if I reload(refresh) the contact page, but if I leave the contact page and visit homepage, and later revisit the contact page, I want it to increment.
In short, I don't want it to increment on page reload. Is there a way to do that?
In each of your pages, you need to write the page name in the session.
Do something like this:
$_SESSION['page'] = 'contact';
On the pages where you need to count hits, you need to check this session key.
For example, if you were on page 'contact', then $_SESSION['page'] == 'contact'.
Now when you go to visit the 'homepage':
$page = $_SESSION['page'];
if($page != 'homepage')
{
//increment your hits counter
$_SESSION['page'] = 'homepage';
}
I suggest this method, is my preferred, create in root these folders: cnt and log... then put inside cnt folder the following files cnt.php and showcnt.php...
cnt.php
<?php
##############################################################################
# Php Counter With Advanced Technology For The Prevention Of Reloading Pages #
# Version: 1.4 - Date: 13.11.2014 - Created By Alessandro Marinuzzi [Alecos] #
##############################################################################
function cnt($file) {
session_start();
global $pagecnt;
$reloaded = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
$thispage = basename($_SERVER['SCRIPT_FILENAME']);
if (!isset($_SESSION['first_go'])) {
$_SESSION['first_go'] = 1;
$first_go = TRUE;
} else {
$first_go = FALSE;
}
if (!isset($_SESSION['thispage'])) {
$_SESSION['thispage'] = $thispage;
}
if ($_SESSION['thispage'] != $thispage) {
$_SESSION['thispage'] = $thispage;
$new_page = TRUE;
} else {
$new_page = FALSE;
}
$pagecnt = rtrim(file_get_contents($file));
if ((!$reloaded) && ($new_page == TRUE) || ($first_go == TRUE)) {
$fd = fopen($file, 'w+');
flock($fd, LOCK_EX);
fwrite($fd, ++$pagecnt);
flock($fd, LOCK_UN);
fclose($fd);
}
}
?>
showcnt.php
<?php
##############################################################################
# Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
##############################################################################
function gfxcnt($file) {
global $number;
$number = rtrim(file_get_contents($file));
$lenght = strlen($number);
$gfxcnt = "";
for ($i = 0; $i < $lenght; $i++) {
$gfxcnt .= $number[$i];
}
$gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
echo $gfxind;
}
?>
Well, then edit your index.php or other php page... and put at the beginning this piece of code:
<?php session_start(); include("cnt/cnt.php"); cnt("log/index.txt"); include("cnt/showcnt.php"); ?>
Well, then edit index.php or other php page... and use this piece of code for reading counter file:
<?php gfxcnt("log/index.txt"); ?>
It's all, I hope you'll find my answer useful :) My counter can write/read multiple php pages...
Source: my blog (https://www.alecos.it/new/101/101.php)
Add session_start(); to the top.
Now change your if to this:
if (!isset($_SESSION['lastpage']) || $_SESSION['lastpage'] != $_SERVER['QUERY_STRING') {
$hits[0]++;
}
$_SESSION['lastpage'] = $_SERVER['QUERY_STRING'];
This will basically force someone to move to another page if they want to increment the counter.
Update the hit count only if the current URL is not stored in $_SESSION['url'].
After updating the hit count, store the current URL in $_SESSION['url'].
I am having problems keeping the main site displayed after redirecting from a mobile site.
If a mobile device is detected it redirects to the mobile site. There is a "main site" link on the mobile site, when clicked, which will take you to the main site. For some reason it wont stay on the main site when you click a link on the main site home page, it redirects back to the Mobile site.
I assume the cookie is not storing correctly.
<?php
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
$allow_mobile = isset($_COOKIE['mobile'])? true:false;
if (isset($_GET['mobile'])) {
if ($_GET['mobile']=='false'){
setcookie("mobile", "");
$allow_mobile = false;
} else {
setcookie("mobile", true, time() + 31536000, "/");
$allow_mobile = true;
}
}
if ($allow_mobile && $detect->isMobile()){
if (!$detect->isTablet()) {
header("Location:http://mobilesite.mobi");
}
}
$not_mobile_cookie = isset($_COOKIE['notmobile'])? true:false;
if (isset($_GET['mobile'])) $not_mobile_cookie = $_GET['mobile'];
if ($not_mobile_cookie==false && $detect->isMobile()){
if (!$detect->isTablet()) {
header("Location:http://mobile.mobi");
}
}
?>
It is probably something simple but I can't see to figure it out.
Thanks!
The key to your question is that mobile devices are still redirected when you click a link on the main site home page.
Your code is testing for a cookie named ['notmobile'] which does not appear to be getting set anywhere. Therefore always evaluates as false, which is why mobile users are being redirected back to the mobile site.
In your code, the purpose of the mobile GET variable is unclear, but taking it to mean that it permits mobile devices to access the main site, I have renamed the variable below to allowMobile.
Assuming Mobile_Detect is functioning correctly, the following code will allow a mobile device to stay on the main site following a allowMobile=true GET request. This can be cancelled with a with allowMobile=false request.
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
// Do we want to allow a mobile to view the main site content?
// If there is a cookie, yes, if not no.
$allow_mobile = (isset($_COOKIE['mobile']) && $_COOKIE['mobile']) ? true : false;
// If there is a GET allowMobile string saying 'false', delete the cookie and deny access
if (isset($_GET['allowMobile']) && $_GET['allowMobile']=='false') {
// Delete a cookie if one exists
setcookie("mobile", "", time()-1, "/");
$allow_mobile = false;
} elseif (isset($_GET['allowMobile']) {
// if there is any other value for allowMobile, set a cookie allowing mobile access
setcookie("mobile", true, time() + 31536000, "/");
$allow_mobile = true;
}
// If we DO NOT allow mobile, then redirect to the mobile site
if (!$allow_mobile && $detect->isMobile() && !$detect->isTablet()){
header("Location: http://mobilesite.mobi");
exit();
}
// Else, display or redirect to non-mobile page here
This seems to work if anyone else has the same problem. I am not sure if it is even the correct way of doing it but it is working properly for me.
Thank you very much for the help PassKit, much appreciated!
<?php
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
$mobile_cookie = isset($_COOKIE['mobile'])? $_COOKIE['mobile'] : "";
$force_mobile = ($mobile_cookie == "true") ? true : false;
if (isset($_GET['mobile'])) {
if ($_GET['mobile'] == 'true') { // must we force the mobile site? if ?mobile=true then FORCE THAT MOBILE
setcookie("mobile", "true", time() + 31536000, "/");
$force_mobile = true;
} else { // if ?mobile=false then remove the force
setcookie("mobile", "false");
$force_mobile = false;
}
}
if ($force_mobile){
header("Location:http://mobilesite.mobi");
} else {
if ($detect->isMobile()){
if ($mobile_cookie == "" && !$detect->isTablet()){
header("Location:http://mobilesite.mobi");
}
}
}
?>
I got a problem with facebook like button and the og:meta tags. What I'm trying to do is to serve the Facebook crawler with different content based on a get parameter.
My page is designed with a landing page and a few product pages. When the user is on the product pages they press the like button and likes the landing page. Then I want to show four different copy text on the users facebook depending on which product that was liked.
When the users friends click the like article on facebook they are redirected to the landing page and I want a general og:meta to be generated.
What I tried is what you see below - but without luck.
$refAddr = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/facebookexternalhit/si',$refAddr)) {
if(isset($_GET['fb_ref'])) {
if($_GET['fb_ref'] == "page1" || $_GET['fb_ref'] == "page2" || $_GET['fb_ref'] == "page3" || $_GET['fb_ref'] == "default") {
$line = $_GET['fb_ref'];
} else {
$line = "default";
}
} else {
$line = "default";
}
} else {
$line = "default";
}