Jump to content

Recommended Posts

Much like adsense, I made an ajax script which would fetch the link from the database using a php file.

But there is a problem with cross domain AJAX requests. They are not allowed. Thus, it renders the whole method useless.

 

So what other method can be implemented to achieve the same. How does adsense does this, any idea?

This code is used to display ad on any site

<script src=http://www.aboutanumber.com/xmlobject.js></script>
<script src=http://www.aboutanumber.com/linkcreator.js></script>
<script type="text/javascript">
linkcreate("http://www.aboutanumber.com/link.php");
function displink(links)
{
document.write("<a href=http://www.aboutanumber.com/"+links+">About "+links+"</a>");
}
</script>

 

 

Linkcreator.js

function linkcreate(url)
{
var xhr=new httprequest();
var url=url+"?domain="+location.href;
xhr.open("GET",url,true);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4 && xhr.status==200)
{
	displink(xhr.responseText);
}
}
xhr.send(null);
}
[/create]


Link.php
[code]
<?php
$con=mysql_connect("localhost","root","") or die("error");
mysql_select_db("links",$con) or die("error");


$domain=$_GET['domain'];
//echo "$domain";
//echo "<script>alert($domain)</script>";


$q="Select page from links where page='$domain'";
$res=mysql_query($q);

if(mysql_num_rows($res))
{
$q2="Select num from links where page='$domain'";
$res2=mysql_query($q2);
$link_num=mysql_fetch_row($res2);
$domain;
echo $link_num[0];
exit;
}
else
{
assign_newnum($domain);
}

function assign_newnum($page)
{
$matched=1;
while($matched)
{	
srand(time());
$random = (rand()%9999)+1;
//$query="select num from links where num='$random'";
$result=mysql_query("select num from links where num='$random'");
$row_found=mysql_num_rows($result);

if($row_found<1)
{
	$matched=0;
	$ins_query="insert into links values('$page','$random')";
	$res_ins_query=mysql_query("insert into links values('$page','$random')");
	echo "$random";
	exit;
}
else
{
	$matched=1;
}
exit;
}
exit;
}


?>

 

 

Link.php checks if the javascript's domain is in the database or not. If it's there, it fetches the link from the database and echoes it on the screen (which is then taken by ajax), if it's not there - it creates a random link and stores it in database.

 

That means if a page is loaded for the first time, a random link is generated by php and stored in database. From then on whenever it is accessed, the link is displayed from the database.

 

Any help would be appreciated

You're over thinking this. You absolutely need javascript but you don't need the Ajax stuff. On the remote server hosting the ads (links) and the javascript you just need something like this:

 

Remote server code

function getLinks()
{
   var links = new Array();
   links[0] = "http://www.google.com";
   links[1] = "http://www.yahoo.com";
   links[2] = "http://www.msn.com";

   var len = links.length;
   for( var i = 0; i < len; ++i )
   {
      document.write("<a href='" + links[i] + "'>" + links[i] + "</a>");
   }
}

 

To use the code

<script type="text/javascript" src="http://www.someotherserver.com/ads.js"></script>
getLinks();

 

This code is untested, but conceptually should be all you need.

You're over thinking this. You absolutely need javascript but you don't need the Ajax stuff. On the remote server hosting the ads (links) and the javascript you just need something like this:

 

Remote server code

function getLinks()
{
   var links = new Array();
   links[0] = "http://www.google.com";
   links[1] = "http://www.yahoo.com";
   links[2] = "http://www.msn.com";

   var len = links.length;
   for( var i = 0; i < len; ++i )
   {
      document.write("<a href='" + links[i] + "'>" + links[i] + "</a>");
   }
}

 

To use the code

<script type="text/javascript" src="http://www.someotherserver.com/ads.js"></script>
getLinks();

 

This code is untested, but conceptually should be all you need.

 

Clearly, you didn't get what I want to acheive. There are 100000 links. Now I can't create an array for that. Even if I can, my client wants it like this. The first time an ad is loaded, a random links from the 100000 links is selected and it is then fixed for that page. From next time, the same link will get loaded on that page.

 

 

 

Google Adsense creates an iframe in which the ads are displayed.

 

Really? I didn't know that.

To use the code

<script type="text/javascript" src="http://www.someotherserver.com/ads.js"></script>
getLinks();

getLinks() is outside a script tag so it won't run.

 

dpacmittal - you can be a little more respectable to people helping you.  :-\  What you can do is have the PHP file handle all PHP and JavaScript output.

To use the code

<script type="text/javascript" src="http://www.someotherserver.com/ads.js"></script>
getLinks();

getLinks() is outside a script tag so it won't run.

 

dpacmittal - you can be a little more respectable to people helping you.  :-\  What you can do is have the PHP file handle all PHP and JavaScript output.

Thanks everyone. I am dropping the ajax idea. What I am doing now is set the Header of php file as

Header("content-type: application/x-javascript");

and echo "var link=$links";

 

But I've got another problem with this, which I will be posting in javascript section.

dpacmittal - you can be a little more respectable to people helping you.  :-\  What you can do is have the PHP file handle all PHP and JavaScript output.

 

You could also post some helpful post instead of just "Cross domain AJAX fail! Forget cross server AJAX. :D"

 

I never posted any rude comments. Please quote them, if I did so.

I could, but I'm not obligated to.

 

Clearly, you didn't get what I want to acheive. There are 100000 links. Now I can't create an array for that. Even if I can, my client wants it like this. The first time an ad is loaded, a random links from the 100000 links is selected and it is then fixed for that page. From next time, the same link will get loaded on that page.

What do you think? A bit harsh? I mean of course we didn't. You haven't told us up to that point.

Not to state the obvious here but you wouldn't create an array with 100000 items. You'd generate an array of 4 items (or whatever number of links you want to display) by programmatically choosing the ones you one from the master list. The code was a quick proof of concept to get you off of the Ajax kick... and Ken2k7 was right, the getLinks() function call was not in script tags.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.