Dynamically generate facebook meta - php

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";
}

Related

Identify referrer based on landing URL

I have noticed that when you click on a Google Ads link, it appends this ?gclid=abcdef on url.
Same behavior goes for facebook as well.
I am using the following block of code in order to identify the referrer.
if (strpos($url, 'gclid') !== false) {
$ref = 'Google Adwords';
} else if (strpos($url, 'fbclid') !== false) {
$ref = 'Facebook';
}
Is there any other url identifiers or an other way to get the referrer url? The $_SERVER['HTTP_REFERER'] does not work.

Change urls depending on landing page

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();

How to redirect login to different pages based on the subpage from my mobile app I am accessing from?

I have a mobile application in HTML5 containing several subpages which have different iframes for each section of my website. The website uses login and this login page is set to automatically redirect to index.php after login is successful.
I would like to set different redirection rules based on which specific subpage from my mobile application I am trying to login from.
Referrer won't work since it's a mobile application and the browser will see the user trying to directly access the iframe from his android browser.
How can I accomplish this?
Here is the redirection code from my login page :
<script>
window.onload = function () {
if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chromee') == -1) {
var cookies = document.cookie;
if (top.location != document.location) {
if (!cookies) {
href = document.location.href;
href = (href.indexOf('?') == -1) ? href + '?' : href + '&';
top.location.href = href + 'reref=' + encodeURIComponent(document.referrer);
}
} else {
ts = new Date().getTime();
document.cookie = 'ts=' + ts;
rerefidx = document.location.href.indexOf('reref=');
if (rerefidx != -1) {
href = decodeURIComponent(document.location.href.substr(rerefidx + 6));
window.location.replace(href);
}
}
}
}
</script>
<?php
if ($user->LoggedIn()) {
echo '<div class="alert alert-success">User already logged in. Redirecting</font></p></center></div>';
echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php\">";
}

Redirect loop in full site to mobile site using session

I have a full site that has been in OS-commerce and mobile site is in core PHP (codeignitor), and full version and a mobile version on sub-domain.
e.g full site: www.example.com and mobile site domain is m.example.com. when user open full site domain in mobile, then website redirect proper mobile domain, But if mobile user want to view full site then user can view fullsite in mobile.
I have used this to complete the redirect http://code.google.com/p/php-mobile-detect/, But it is not redirecting to the full site or to the mobile site using session. I know that I have to use PHP SESSIONS and REQUEST in order to get this to work but I am not sure how to use them in this instance, so could you please suggest how to solve this redirecting issue using session?
Here my code is:
session_start();
include('includes/Mobile_Detect.php');
$detect = new Mobile_Detect;
if(isset($_REQUEST['fullsite']) && $_REQUEST['fullsite'] == 'yes')
{//check if fullsite view request from mobile or website?
$_SESSION['fullsite']="yes";
if($detect->isMobile()) {
$_SESSION['website']="mobile";
}
else{
$_SESSION['website']="computer";
}
$deviceType = header('Location: https://www.example.com/');
}
else
{
if($_SESSION['website'] =="mobile" && $_SESSION['fullsite'] !="yes")
{
if($detect->isTablet())
{
$deviceType = 'tablet';
}
else
{
$deviceType = 'phone';
}
$deviceType = header('Location: https://m.example.com/');
}
elseif($_SESSION['website'] =="computer" && $_SESSION['fullsite'] =="yes")
{
$deviceType = 'computer';
$deviceType = header('Location: https://www.example.com/');
}
else{
$deviceType = 'computer';
}
$scriptVersion = $detect->getScriptVersion();
session_destroy();
}
From what I could get from github page you should be able to make it work like this:
index.php
session_start();
if ($_GET['fullscreen'] == 'yes') {
$_SESSION['fullscreen'] = 1;
} else if ($_GET['fullscreen'] == 'no') {
$_SESSION['fullscreen'] = 0;
}
if (false == isset($_SESSION['fullscreen']) && ($_SESSION['fullscreen'] == 0)) {
// If session['fullscreen'] has not been set (maybe first visit
// or the user does not what in fullscree
// check the device and do redirect
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect();
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
...
}
// Other code here
When visiting from mobile, if the user wants the full version, provide an anchor to url with GET parameter fullscreen=yes (http://example.com?fullscreen=yes)
If on full site and detect mobile (not included in code above), you could provide a link to mobile version with fullscreen=no

Mobile site only redirects to main site home page

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");
}
}
}
?>

Categories