Jump to content

Google AdSense Implementation


supertrucker
 Share

Recommended Posts

I don't know if anybody here has or uses Google AdSense, but it's pretty straightforward, at least it was when I used it in the past. However, I just pasted the Google code into my php script, and now I'm getting this:

Notice: Undefined index: HTTPS in /zerorubbish/test.php on line 36Notice: Undefined index: HTTP_REFERER in /zerorubbish/test.php on line 42Notice: Undefined index: HTTP_UA_PIXELS in /zerorubbish/test.php on line 64Notice: Undefined index: HTTP_X_UP_DEVCAP_SCREENPIXELS in /zerorubbish/test.php on line 67

Any ideas? Need anymore information to help me?

Link to comment
Share on other sites

Sure :)

<?php$GLOBALS['google']['ad_type']='text_image';$GLOBALS['google']['channel']='7310528573';$GLOBALS['google']['client']='pub-7623739021471565';$GLOBALS['google']['format']='mobile_single';$GLOBALS['google']['https']=$_SERVER['HTTPS'];$GLOBALS['google']['host']=$_SERVER['HTTP_HOST'];$GLOBALS['google']['ip']=$_SERVER['REMOTE_ADDR'];$GLOBALS['google']['markup']='xhtml';$GLOBALS['google']['oe']='utf8';$GLOBALS['google']['output']='xhtml';$GLOBALS['google']['ref']=$_SERVER['HTTP_REFERER'];$GLOBALS['google']['url']=$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];$GLOBALS['google']['useragent']=$_SERVER['HTTP_USER_AGENT'];$google_dt = time();google_set_screen_res();function google_append_url(&$url, $param, $value) {  $url .= '&' . $param . '=' . urlencode($value);}function google_append_globals(&$url, $param) {  google_append_url($url, $param, $GLOBALS['google'][$param]);}function google_append_color(&$url, $param) {  global $google_dt;  $color_array = split(',', $GLOBALS['google'][$param]);  google_append_url($url, $param,					$color_array[$google_dt % sizeof($color_array)]);}function google_set_screen_res() {  $screen_res = $_SERVER['HTTP_UA_PIXELS'];  $delimiter = 'x';  if ($screen_res == '') {	$screen_res = $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'];	$delimiter = ',';  }  $res_array = explode($delimiter, $screen_res);  if (sizeof($res_array) == 2) {	$GLOBALS['google']['u_w'] = $res_array[0];	$GLOBALS['google']['u_h'] = $res_array[1];  }}function google_get_ad_url() {  $google_ad_url = 'http://pagead2.googlesyndication.com/pagead/ads?';  $google_scheme = ($GLOBALS['google']['https'] == 'on')	  ? 'https://' : 'http://';  foreach ($GLOBALS['google'] as $param => $value) {	if ($param == 'client') {	  google_append_url($google_ad_url, $param,						'ca-mb-' . $GLOBALS['google'][$param]);	} else if (strpos($param, 'color_') === 0) {	  google_append_color($google_ad_url, $param);	} else if ((strpos($param, 'host') === 0)			   || (strpos($param, 'url') === 0)) {	  google_append_url($google_ad_url, $param,						$google_scheme . $GLOBALS['google'][$param]);	} else {	  google_append_globals($google_ad_url, $param);	}  }  google_append_url($google_ad_url, 'dt',   			round(1000 * array_sum(explode(' ', microtime()))));  return $google_ad_url;}$google_ad_handle = @fopen(google_get_ad_url(), 'r');if ($google_ad_handle) {  while (!feof($google_ad_handle)) {	echo fread($google_ad_handle, 8192);  }  fclose($google_ad_handle);}?>

Google provides the code, all I have to do is copy and paste it into my app. Somethings not right though...

Link to comment
Share on other sites

Google should know better. You can add code to check if those things are set before using them, there's no guarantee that any of them will be set. So these lines:$GLOBALS['google']['https']=$_SERVER['HTTPS'];$GLOBALS['google']['ref']=$_SERVER['HTTP_REFERER'];$screen_res = $_SERVER['HTTP_UA_PIXELS'];$screen_res = $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'];would become these:

$GLOBALS['google']['https']=isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : '';$GLOBALS['google']['ref']=isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';$screen_res = isset($_SERVER['HTTP_UA_PIXELS']) ? $_SERVER['HTTP_UA_PIXELS'] : '';$screen_res = isset($_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS']) ? $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'] : '';

Link to comment
Share on other sites

Thanks :)I modified your code as per suggestion, and the errors went away. However, ads are not showing up. Argh!ST

Link to comment
Share on other sites

Hmm, those changes shouldn't affect that. Post your new code if you want.
<?php$GLOBALS['google']['ad_type']='text';$GLOBALS['google']['channel']='7310528573';$GLOBALS['google']['client']='pub-7623739021471565';$GLOBALS['google']['format']='mobile_single';$GLOBALS['google']['https']=isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : '';$GLOBALS['google']['host']=$_SERVER['HTTP_HOST'];$GLOBALS['google']['ip']=$_SERVER['REMOTE_ADDR'];$GLOBALS['google']['markup']='xhtml';$GLOBALS['google']['oe']='utf8';$GLOBALS['google']['output']='xhtml';$GLOBALS['google']['ref']=isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';$GLOBALS['google']['url']=$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];$GLOBALS['google']['useragent']=$_SERVER['HTTP_USER_AGENT'];$google_dt = time();google_set_screen_res();function google_append_url(&$url, $param, $value) {  $url .= '&' . $param . '=' . urlencode($value);}function google_append_globals(&$url, $param) {  google_append_url($url, $param, $GLOBALS['google'][$param]);}function google_append_color(&$url, $param) {  global $google_dt;  $color_array = split(',', $GLOBALS['google'][$param]);  google_append_url($url, $param,					$color_array[$google_dt % sizeof($color_array)]);}function google_set_screen_res() {  $screen_res = isset($_SERVER['HTTP_UA_PIXELS']) ? $_SERVER['HTTP_UA_PIXELS'] : '';  $delimiter = 'x';  if ($screen_res == '') {	$screen_res = isset($_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS']) ? $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'] : '';	$delimiter = ',';  }  $res_array = explode($delimiter, $screen_res);  if (sizeof($res_array) == 2) {	$GLOBALS['google']['u_w'] = $res_array[0];	$GLOBALS['google']['u_h'] = $res_array[1];  }}function google_get_ad_url() {  $google_ad_url = 'http://pagead2.googlesyndication.com/pagead/ads?';  $google_scheme = ($GLOBALS['google']['https'] == 'on')	  ? 'https://' : 'http://';  foreach ($GLOBALS['google'] as $param => $value) {	if ($param == 'client') {	  google_append_url($google_ad_url, $param,						'ca-mb-' . $GLOBALS['google'][$param]);	} else if (strpos($param, 'color_') === 0) {	  google_append_color($google_ad_url, $param);	} else if ((strpos($param, 'host') === 0)			   || (strpos($param, 'url') === 0)) {	  google_append_url($google_ad_url, $param,						$google_scheme . $GLOBALS['google'][$param]);	} else {	  google_append_globals($google_ad_url, $param);	}  }  google_append_url($google_ad_url, 'dt',   			round(1000 * array_sum(explode(' ', microtime()))));  return $google_ad_url;}$google_ad_handle = @fopen(google_get_ad_url(), 'r');if ($google_ad_handle) {  while (!feof($google_ad_handle)) {	echo fread($google_ad_handle, 8192);  }  fclose($google_ad_handle);}?>

Link to comment
Share on other sites

Also one more thing on this, I think it might have something to do with the fopen command, as a script plugin I have in WordPress says that fopen is not enabled. I looked at my php.ini file and it is enabled... ...this have something to do with it?

Link to comment
Share on other sites

Okay, I removed the "@" with no apparent effect thus far. Here is the php.ini section on fopen:

;;;;;;;;;;;;;;;;;;; Fopen wrappers;;;;;;;;;;;;;;;;;;;; Whether to allow the treatment of URLs (like http:// or ftp://) as files.allow_url_fopen = On; Define the anonymous ftp password (your email address);from="john@doe.com"; Define the User-Agent string; user_agent="PHP"; Default timeout for socket based streams (seconds)default_socket_timeout = 60; If your scripts have to deal with files from Macintosh systems,; or you are running on a Mac and need to deal with files from; unix or win32 systems, setting this flag will cause PHP to; automatically detect the EOL character in those files so that; fgets() and file() will work regardless of the source of the file.; auto_detect_line_endings = Off

According to this, fopen is on, do I maybe need to change one of these other settings, or perhaps another php.ini setting?

Link to comment
Share on other sites

It's in a plugin that I downloaded that converts my WordPress blog into a mobile version on the fly. Wierd, it was saying that fopen was not enabled, now it doesn't say that...

Link to comment
Share on other sites

  • 1 month later...

I'm revisiting this issue because the Google AdSense is still not working. I'm wondering, if as the errors above, that are now hidden because of the "@" are causing these ads not to display. So, what I'm getting at, is there an alternative to using the $_SERVER variables:

  $_SERVER['HTTP_UA_PIXELS'];  $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'];  $_SERVER['HTTPS'];  $_SERVER['HTTP_REFERER'];

Or, dirty/alternatively a way to reliably force them somehow? Like, for instance, how do you detect screen resolution on your client, apparently Google needs this information to size the ad content appropriately. If I can force them, what should I put for these variables?Thank you,ST

Link to comment
Share on other sites

Well, since the variables are not set you get:

Notice: Undefined index: HTTP_UA_PIXELS in /hermes/bosweb/web237/b2379/sl.supertru/public_html/zerorubbish/test.php on line 17Notice: Undefined index: HTTP_X_UP_DEVCAP_SCREENPIXELS in /hermes/bosweb/web237/b2379/sl.supertru/public_html/zerorubbish/test.php on line 18Notice: Undefined index: HTTPS in /hermes/bosweb/web237/b2379/sl.supertru/public_html/zerorubbish/test.php on line 19Notice: Undefined index: HTTP_REFERER in /hermes/bosweb/web237/b2379/sl.supertru/public_html/zerorubbish/test.php on line 20

I get what the screen resolution variables are asking for, but I don't quite get what the HTTPS, or HTTP_REFERER are supposed to be, what do those variables usually return? I'm guessing referer would be the address of my site?

Link to comment
Share on other sites

HTTPS is set if the user is connected using HTTPS. The referer is the page that contained the link that you clicked to get to the current page. I'm not aware of any way for PHP to get the client's resolution without using something like Javascript.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...