dpacmittal Posted May 11, 2009 Share Posted May 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/ Share on other sites More sharing options...
dbo Posted May 11, 2009 Share Posted May 11, 2009 Adsense doesn't use Ajax. It's just a hunk of javascript that will barf out the ads on the screen. With this method you won't have any cross site request errors. Perhaps if you show some code we could be of more help. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831350 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831362 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 Cross domain AJAX fail! Forget cross server AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831366 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 Cross domain AJAX fail! Forget cross server AJAX. So how do I achieve what I want to? Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831376 Share on other sites More sharing options...
dbo Posted May 11, 2009 Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831499 Share on other sites More sharing options...
Daniel0 Posted May 11, 2009 Share Posted May 11, 2009 Google Adsense creates an iframe in which the ads are displayed. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831527 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831581 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831632 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831648 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 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. " I never posted any rude comments. Please quote them, if I did so. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831654 Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831658 Share on other sites More sharing options...
dpacmittal Posted May 11, 2009 Author Share Posted May 11, 2009 Yeah.. sorry for that. It actually is a bit rude. I just found that out on re-reading it. But I never felt so while posting. And I didn't meant to be rude. Sorry for that. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-831662 Share on other sites More sharing options...
dbo Posted May 11, 2009 Share Posted May 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157656-i-want-to-display-ads-from-my-server-using-javascript/#findComment-832021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.