ITChristian
ITChristian

Reputation: 11271

Get a Javascript variable in PHP

I have a simple javascript code, that tells me if my website is in iframe.

<script>
if ( window.self === window.top ) { 
    var iframe = '0';
} else { 
    var iframe = '1';
}
</script>

How can I get the "iframe" variable from javascript, in php?

I want to do something like this:

<?php if ($iframe == "0") {
 //mysite for iframe
} else {
 //mysite general
}
?>

Is this possible?

PS.: I want to be SEO, too.

Thank you so much!!!

Upvotes: 1

Views: 12454

Answers (8)

Swapnil Dalvi
Swapnil Dalvi

Reputation: 1029

You can do one thing make ajax call and send that variable to php file like At your javascript side==

jQuery.ajax({
            type: 'get', // type of request either Get or Post
            url: 'getValue.php', // Url of the file to set value in Session  
            data: "value="+iframe,  //that value which you want to use in php  
            success: function(data) 
            {}  });

At your php side

<?php
session_start();
$val = $_GET["value"]; //receive data from ajax call
$_SESSION['value']=$val; //set that value in session
echo $_SESSION['value']; //way to access value which is stored in session
?>

now to access that value in php just need to write on the start of php file
session_start();
and access that variable using
$_SESSION['value'];
there is also another way i.e pass that variable through url e.g "http://www.example.com/index.php?value=123456"
in your index.php
$received_fr_url=$_REQUEST['value']; will take the value from url

Upvotes: 0

nl-x
nl-x

Reputation: 11832

Since the order is:

-first parse PHP and send HTML to client
-then client runs Javascript that is in HTML

to do what you want will take multiple requests.

What you could do is:

-Load an HTML page that uses Ajax (a second request) to send the javascript variable to the server. The server then responds to the Ajax request with XML, and javascript should already know what to do with the answer.
-Load an HTML page that directly refreshes itsself, but sends along the javascript variable as a request variable (POST or GET). Something like:

<?php
    if (!isset($_REQUEST['iframe']) { // php checks if this it got the variable
        // it is not given yet, so let javascript refresh and give the variable
?>
        <script>
            var url = window.location.href.split('#')[0];
            var devider = url.indexOf('?') == -1 ? '?' : '&';
            var hash = typeof window.location.href.split('#')[1] == 'undefined' ? '' : '#' + window.location.href.split('#')[1];
            if ( window.self === window.top ) {
                window.location.href = url + devider + 'iframe=0' + hash;
            } else { 
                window.location.href = url + devider + 'iframe=1' + hash;
            }
        </script>
<?php
    } else {
        // it is given to php, lets do something with it...
        if ($_REQUEST['iframe'] == 1) {
            // ok, we're in an iframe. So what do you want to do?
        } else {
            // ok, we're not in an iframe. So what do you want to do?
        }
    }
?>

Upvotes: 3

ITChristian
ITChristian

Reputation: 11271

You can simple use:

<script>
    if ( window.self === window.top ) { 
            window.location = window.location + '?iframe=0';
    } else { 
            window.location = window.location + '?iframe=1';
    }
</script>

You said that you want to be SEO too, so you can put a canonical link rel in <head></head>:

<?php
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$canonical = 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];
?>

 <link rel="canonical" href="<?php echo $canonical; ?>"/>

Upvotes: 3

Eugene
Eugene

Reputation: 1152

Looks like you can't. But you can send it with ajax to server and load requested changes:

if ( window.self === window.top ) { 
    var iframe = '0';
} else { 
    var iframe = '1';
    // here ajax requested that will load instructions about how to change a page
}

Or u can use window.location = url; for change url of page. For example you can add get parameter ?iframe=true and then use it in php:

if (isset($_GET['iframe'])) {
    // here something
}

Upvotes: 2

Dave Chen
Dave Chen

Reputation: 10975

Check out $_SERVER['HTTP_REFERER'], if the page is loaded from the frame, the refer should be different from the frame's src.

A quick test:

main.html:

<iframe src="frame.php"></iframe>

frame.php

echo $_SERVER['HTTP_REFERER'];

Visiting the page at main.html shows localhost/main.html while visiting at frame.php shows localhost/frame.php.

This is simply for SEO purposes, as this variable shouldn't be trusted for security reasons.

Upvotes: 2

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Check this out: Ajax

This is a short example of how to do Ajax calls, as PHP is run on the serverside, whereas javscript is clientside.

So essentially, your client function sends a request to the server, which then executes one of you PHP-implemented functions and returns the result.

Upvotes: 2

Iesus Sonesson
Iesus Sonesson

Reputation: 854

As Javascript is client side code and PHP is Server side code, the variables must somehow be passed to the server.

There are a few decent ways of doing this, by far the most common is GET and POST variables. then you can pick them up in php and do whatever you wish with it.

You may pass variables like this: file.php?EEEE=YYY then fetch that variable with

$_GET['EEEE']

Upvotes: 2

PherricOxide
PherricOxide

Reputation: 15919

PHP is server side, Javascript is client side. You'll have to do something like Ajax to transfer the value from the web browser (Javascript) to the web server (PHP).

Upvotes: 3

Related Questions