Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need help converting javascript to PHP
Old 05-13-2009, 11:00 AM Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
Code:
<script type="text/javascript">
var source="http://www.website.com/link.php?domain="+location.href;
document.write('<script type="text/javascript" src="' + source + '"><\/script>');
</script>
<script type="text/javascript">
document.write("<a href=http://www.website.com/"+link+">"+link+"</a>");
</script>
So I hired a programmer to make me a script that creates urls for my domain based on a php script that queries a database to get them.

But apparently javascript doesnt pass link info in search engines so it totally messes with my navigation system plans. So now I think I need to convert this to a PHP call so that Search engine bots actually see the real link that I want.


Could anyone help me convert this code into php? I am only a very unskilled programmer so this is way out of my league. Thanks!
sparkroms is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-13-2009, 11:31 AM Re: Need help converting javascript to PHP
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You aren't showing us a complete script. We can't have any idea where the link variable is coming from or how to retrieve it.

We need to see the script that is at:

http://www.website.com/link.php?domain="+location.href;

To retrieve it type into your web browser:

http://www.website.com/link.php?domain=

Followed by the exact URL for the page you are currently on.

Or better yet, show us the PHP that is on "link.php"
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-13-2009, 11:37 AM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
I did not realize it was required, sorry.

Code:
<?php
Header("content-type: application/x-javascript");
$con=mysql_connect("localhost","username","pass") or die("var link=\"error connecting to db\"s");
mysql_select_db("db_name",$con) or die("var link=\"error selecting the db\"");


$domain=$_GET['domain'];


$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);
echo "var link=$link_num[0]";
exit;
}
else
{
assign_newnum($domain);
}

function assign_newnum($page)
{
$matched=1;
while($matched)
{   
    srand(time());
    $random = (rand()%99999)+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 "var link=$random";
        exit;
    }
    else
    {
        $matched=1;
    }
exit;
}
exit;
}


?>
does that help?

also the output

for
http://www.website.com/link.php?domain=www.test2.com

is
var link=9224 (its any number between 0 and 100,000)

Last edited by sparkroms; 05-13-2009 at 11:38 AM..
sparkroms is offline
Reply With Quote
View Public Profile
 
Old 05-13-2009, 10:12 PM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
So, is this actually a hard job since no one is responding? I thought it was gonna be easy :-\
sparkroms is offline
Reply With Quote
View Public Profile
 
Old 05-13-2009, 11:23 PM Re: Need help converting javascript to PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
If this is going to be on other websites that are not yours, this will not work. You would have to make a requirement for the other linking sites to insert php in thier pages.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-14-2009, 12:00 AM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
Yeah I will have the other sites put in the code as php. That should work then right?
sparkroms is offline
Reply With Quote
View Public Profile
 
Old 05-14-2009, 09:05 PM Re: Need help converting javascript to PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I would think that trying to get others to implement php for your links would not be a great idea, and a big hassel for most. I thought that maybe an inline frame may work because search engines would see that (however the links would come from your own domain).

Just have the users to edit the domain=ENTER_REFERRING_DOMAIN_HERE to domain=www.domain.com


HTML Code:
<iframe src="http://www.website.com/link.php?domain=ENTER_REFERRING_DOMAIN_HERE" width="100%" height="25" scrolling="no" frameborder="0">
  <a href="http://www.website.com/">000</a>
</iframe>

I reconstructed your php script. I don't understand why you would use a random number id rather than using a incremented id value. It would stop having to require iterating finding a integer that was not being used, and the rows would be easily sorted in order as they were originally created.


PHP Code:
<?php
  
  $db 
= @mysql_connect('localhost''username''password') OR die('error connecting to db');
  
mysql_select_db('db_name'$db) OR die('error selecting the db');
  
  
// Set vars to be used:
  
$domain '';
  
$link_id 0;
  
  
// Check if domain was defined as a parameter:
  
if (isset($_GET['domain']) AND preg_match('/^www\.[A-Z-_]+\.[A-Z]{2,}$/i'$_GET['domain']))
  {
    
$domain strtolower($_GET['domain']);
  }
  
  
// Check referrer if domain was not defined (used as a backup resource):
  
if (!$domain AND isset($_SERVER['HTTP_REFERER']) AND !empty($_SERVER['HTTP_REFERER']))
  {
    if (
preg_match('@^.*(://)?(www\.[A-Z-_]+\.[A-Z]{2,})/.*$@i'$_SERVER['HTTP_REFERER'], $match))
    {
      
$domain strtolower($match[2]);
    }
  }
  
  if (
$domain)
  {
    
// Check if referrer exists in the db:
    
$db_query mysql_query("
      SELECT num
      FROM links
      WHERE page = '" 
mysql_real_escape_string($domain) . "'
    "
);
    
    if (
mysql_num_rows($db_query) > 0)
    {
      
// Link does exist:
      
$db_result mysql_fetch_array($db_query);
      
$link_id $db_result['num'];
    }
    else
    {
      
// Link does not exist - create it:
      
while (true)
      {
        
srand(time());
        
$link_id = (rand()%99999)+1;
        
        
$db_query mysql_query("
          SELECT COUNT(*) AS count
          FROM links
          WHERE num = 
$link_id
        "
);
        
        
$db_result mysql_fetch_array($db_query);
        
        if (!
$db_result['count'])
        {
          
mysql_query("
            INSERT INTO links (num, page)
            VALUES (
$link_id, '" mysql_real_escape_string($domain) . "')
          "
);
          
          break;
        }
      }
    }
  }
  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title></title>
  </head>
  <body>
    <a href="http://www.website.com/<?php if ($domain) echo $link_id?>"><?php echo ($domain) ? $link_id '000'?></a>
  </body>
</html>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 05-14-2009 at 09:12 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-14-2009, 09:37 PM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
Isn't google not going to count these as real backlinks. I don't think iframes are seo friendly.
sparkroms is offline
Reply With Quote
View Public Profile
 
Old 05-14-2009, 10:01 PM Re: Need help converting javascript to PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by sparkroms View Post
Isn't google not going to count these as real backlinks. I don't think iframes are seo friendly.
Yeah, your probably right. I guess you can have your users use this php snippet:

PHP Code:
<?php echo ($link file_get_contents('http://www.website.com/link.php?domain=ENTER_REFERRING_DOMAIN_HERE')) ? $link '<a href="http://www.website.com/">000</a>'?>

And your script on your domain:

PHP Code:
<?php
  
  $db 
= @mysql_connect('localhost''username''password') OR die('error connecting to db');
  
mysql_select_db('db_name'$db) OR die('error selecting the db');
  
  
// Set vars to be used:
  
$domain '';
  
$link_id 0;
  
  
// Check if domain was defined as a parameter:
  
if (isset($_GET['domain']) AND preg_match('/^www\.[A-Z-_]+\.[A-Z]{2,}$/i'$_GET['domain']))
  {
    
$domain strtolower($_GET['domain']);
  }
  
  if (
$domain)
  {
    
// Check if referrer exists in the db:
    
$db_query mysql_query("
      SELECT num
      FROM links
      WHERE page = '" 
mysql_real_escape_string($domain) . "'
    "
);
    
    if (
mysql_num_rows($db_query) > 0)
    {
      
// Link does exist:
      
$db_result mysql_fetch_array($db_query);
      
$link_id $db_result['num'];
    }
    else
    {
      
// Link does not exist - create it:
      
while (true)
      {
        
srand(time());
        
$link_id = (rand()%99999)+1;
        
        
$db_query mysql_query("
          SELECT COUNT(*) AS count
          FROM links
          WHERE num = 
$link_id
        "
);
        
        
$db_result mysql_fetch_array($db_query);
        
        if (!
$db_result['count'])
        {
          
mysql_query("
            INSERT INTO links (num, page)
            VALUES (
$link_id, '" mysql_real_escape_string($domain) . "')
          "
);
          
          break;
        }
      }
    }
  }
  
  echo 
'<a href="http://www.website.com/' . (($domain) ? $link_id '000') . '">' . (($domain) ? $link_id '000') . '</a>';
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-16-2009, 03:34 PM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
Thanks for the attempt but that didnt output anything or do anything.

Also, the php code on my domain is supposed to select a random number because that's going to be the anchor text for the link (my website is about numbers). Please let me know if thats doable.

The php code, not on my server, should output

domain.com/(random number based on referring url so that it stays the same in the future)

Thanks a lot for the help though I really appreciate it. I feel like we are almost there.
sparkroms is offline
Reply With Quote
View Public Profile
 
Old 05-16-2009, 07:53 PM Re: Need help converting javascript to PHP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Probably need another method than file_get_contents()
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-17-2009, 12:31 PM Re: Need help converting javascript to PHP
Novice Talker

Posts: 8
Trades: 0
What do you mean? As I said before I am not really a programmer so I don't know where to get another method.
sparkroms is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help converting javascript to PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42196 seconds with 12 queries