pass variables from php to javascript

1

Hi, I would like some help with this question. I have this php code but I would like to pass it to javascript only possible

$referr = $_SERVER['HTTP_REFERER'];
$keyface  = 'facebook';
$desdefacebook = strpos($referr, $keyface);
if ($desdefacebook === false) {
echo "No viene de facebook :D ";
} 
elseif ($desdefacebook !== false) {
   echo "Si viene de facebook";
}  
    
asked by BotXtrem Solutions 02.03.2017 в 21:40
source

3 answers

2

Good morning.

To get the $_SERVER['HTTP_REFERER']; but in javascript, you can access the "referrer" property of the page where you are, as follows:

var result = document.referrer.split( '/' ).indexOf("www.facebook.com");;
if (result >= 0){
    console.log("viene desde fb");
} else {
    console.log("no viene desde fb");
}
    
answered by 02.03.2017 / 21:53
source
1

You can use document.referrer to get the url from which the user came. It would be something like:

var referr = document.referrer; 

if(referr.indexOf('facebook')!==-1) {
  console.log('NO viene desde FB');
} else {
  console.log('Viene desde FB');
}

Obviously document.referrer is an empty string if the user wrote the url by hand or came from their favorites.

PD : @ArieCwHat's response is more correct because it considers only the Facebook host. In my answer there could be false positives if the rest of the URI contains that word. Peeero, I just transformed your code, which has the same qualification.

    
answered by 02.03.2017 в 21:55
0

Here is a way that works for me thanks for your answers.

<div style="display:none;" id="nombre">algo</div>
<script>
var x = document.referrer;
if(x.indexOf("facebook")>0){
document.getElementById("nombre").style.display="block";
}
</script>
    
answered by 24.08.2017 в 03:58