Forum Moderators: coopster

Message Too Old, No Replies

Any way to use a variable from the main page in an include?

Been looking around for an answer...

         

HughMungus

1:22 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...and I don't think it's possible since (I think) whatever is in an include is processed before the main part of the page (e.g., Javascript). Is this correct?

I looked into using a session variable, too, but that looks like it won't work, either.

What I'm trying to do is make my adsense "alternate URL" which is javascript use a variable.

Birdman

1:36 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I'm looking into the same thing. I want to serve different ad formats based on window width.

You are correct, it's too late to do it with server-side scripts.

You could(BUT DON'T) load both versions into js variables on the server and then use js to serve the proper one. However, you would need to "alter" the Adsense code to do it.

The only other option I see is using sessions, and you'll have to wait for the second page load for that to take affect.

I'm all ears for a solution to this!

timster

7:37 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, this is nutty idea:

The first thing your PHP page does is check for a session variable with the monitor size.

If it doesn't find it (e.g., when the visitor first arrives) PHP outputs a blank page, which runs a Javascript to determine the monitor size, and then sends that data back to the PHP page with the monitor size info in the query string.

If it does find the session info, PHP generates the page with the correct info for the monitor size (using a PHP variable to place the correct monitor info into JavaScript).

httpwebwitch

7:59 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it's not so nutty - I've done it, and it works. Put browser specs into a session. If the session isn't there, write one, and reload. Only problem is if sessions aren't working right, you get a reloading loop.

But - there is another way. Put your banner in an iframe, and use javascript to populate it with whatever banner will fit best in the space provided.

<iframe name="myframe" width="100%" height="80">
<script>
if (screen.width<600){
myframe.src="smallbanner.php";
}else{
myframe.src="largebanner.php";
}
</script>

httpwebwitch

8:08 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



not sure if this is obvious, but your adSense alternate URL can be a totally PHP-ized script. Mine is called "fake_adsense.php" and it polls a database to grab 4 random products from the shopping cart to put them in a Google-ish looking PSA replacement.

I see what you mean though about passing a client (javascript) variable into the ad. Maybe you could write a javascript variable on the main page

<script>
window.top.myvariable='hello world';
</script>

then retrieve it in the ad

<script>
alert(window.top.myvariable);
</script>

HughMungus

9:58 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



not sure if this is obvious, but your adSense alternate URL can be a totally PHP-ized script.

Yes, I realized that after I posted the question (the current include is .inc and the PHP I tried isn't working probably because it's not being parsed - duh).

I'm going to try using a PHP page with a database call in it as the alternate URL.

Thanks!