Jump to content

return link checking


chris_s_22

Recommended Posts

im trying to allow users to add links to my links page themselves. but only display links where i have a rteturn link back to me

ive created the table in database, the form, a page to check form data and then inserts data into database, ive got a page that displays the checked links

 

but now im trying to do the return link checking, i carnt test it till later on so in the mean time id be grateful if someone could go through and check my code

im only a php beginner so expect silly mistakes

ive tried to be as descriptive as i can to what im doing.

<?php
// FIRST I WANT TO GET LINKFROM COLUMN DATA FROM DATABASE - THIS CONTAINS URL's TO PAGES WHERE I WOULD EXPECT A RETURN LINK BACK TO MY SITE
$query = ("SELECT linkfrom FROM links");
$result= mysql_query ($query) or die ('Could not query.');

// I THEN WANT TO CHECK EACH ROW 
while ($row = mysql_fetch_assoc($result))
{
// SO I DEFINE MY URL
$mylink = "http://www.yoururlhere.com";
// FROM THE URL STORED IN DATABASE COLULMN GET THE URL PAGE RETURN AS A STRING
$linkcheckcontents = file_get_contents ($result);
if ($linkcheckcontents == FALSE) echo 'Could not open';
else 
{	
	//CHECKS THE LINK RETURNED AS A STRING CONTAINS MY $MYLINK
	if( (stristr($linkcheckcontents, '<a href="'.$mylink) === FALSE) && (stristr($linkcheckcontents, "<a href='" .$mylink) === FALSE)) 
	{
// IF $MYLINK DOESNT APPEAR SET checked='0'
	$query = ("UPDATE links SET checked='0'");
	}
	else
	{
// IF $MYLINK APPEARS SET checked='1'
	$query = ("UPDATE links SET checked='1'");		
	}
}
// GOES BACK TO START AND REPEATS FOR NEXT ROW	
}
?>

 

if you do find any mistakes id be very gratful if you could explain why its wrong

thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/
Share on other sites

$linkcheckcontents = file_get_contents ($result);

This is wrong, you are trying to fetch the contents of a resource object. You should be passing file_get_contents something along the lines of $row['linkfrom'].

 

$query = ("UPDATE links SET checked='0'");

Without a WHERE clause this query would updatel update every row in your database, which I'm assuming you don't want to do. You are also never actually running the query using mysql_query. Obviously the same also applies to...

 

$query = ("UPDATE links SET checked='1'");  

Your code doesn't connect to the database, but I assume your aware of that. This also assumes that the link in your database is the exact page that they intend to put your link on, as it will only search a singular page. Finally, what happens if the other site has the same system as you, neither of you will ever appear on either site.

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/#findComment-965939
Share on other sites

ok thx for advice first i changed the select query not sure if that makes a difference

 

i then as you advised changed

$linkcheckcontents = file_get_contents ($result);

this i totally understand

 

regards to the 2 queries i added a line underneath each

im a bit unsure what i would put in the where statement

so i added

$linkto = $row["linkto"];
then 
$query = ("UPDATE links SET checked='1' WHERE linkfrom='$linkfrom'");	

 

is this fine can you see anything else? this is full code im using

<?php
// FIRST I WANT TO GET LINKFROM COLUMN DATA FROM DATABASE - THIS CONTAINS URL's TO PAGES WHERE I WOULD EXPECT A RETURN LINK BACK TO MY SITE
$query = ("SELECT * FROM links");
$result= mysql_query ($query) or die ('Could not query.');

// I THEN WANT TO CHECK EACH ROW 
while ($row = mysql_fetch_assoc($result))
{
$linkto = $row["linkto"];
// SO I DEFINE MY URL
$mylink = "http://www.yoururlhere.com";
// FROM THE URL STORED IN DATABASE COLULMN GET THE URL PAGE RETURN AS A STRING
$linkcheckcontents = file_get_contents $row['linkfrom']);
if ($linkcheckcontents == FALSE) echo 'Could not open';
else 
{	
	//CHECKS THE LINK RETURNED AS A STRING CONTAINS MY $MYLINK
	if( (stristr($linkcheckcontents, '<a href="'.$mylink) === FALSE) && (stristr($linkcheckcontents, "<a href='" .$mylink) === FALSE)) 
	{
// IF $MYLINK DOESNT APPEAR SET checked='0'
	$query = ("UPDATE links SET checked='0' WHERE linkfrom='$linkfrom'");
	$result= mysql_query ($query) or die ('Could not query.');
	}
	else
	{
// IF $MYLINK APPEARS SET checked='1'
	$query = ("UPDATE links SET checked='1' WHERE linkfrom='$linkfrom'");
	$result= mysql_query ($query) or die ('Could not query.');		
	}
}
// GOES BACK TO START AND REPEATS FOR NEXT ROW	
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/#findComment-965943
Share on other sites

Yes, providing 'linkfrom' is unique, which I assume it should be thats better. You still aren't actually running any queries though. You are just storing a string of the query in a variable. You should be doing...

 

$result = mysql_query("SOME QUERY HERE");

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/#findComment-965945
Share on other sites

i was under the impression that the method you shown with one line is the same as the way i do it in 2 and both ways still run the query or am i wrong

 

$result = mysql_query("UPDATE links SET checked='0' WHERE linkfrom='$linkfrom'");

 

 

$query = ("UPDATE links SET checked='0' WHERE linkfrom='$linkfrom'");	    
$result= mysql_query ($query) or die ('Could not query.');

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/#findComment-965979
Share on other sites

Doing it internally or as a seperate variable doesn't matter, you just need to remember to store the result in a difference variable. Strictly speaking you don't actually have to capture the result of the UPDATE statements since you never do anything with $result. As you have it you could just do...

 

$query = "UPDATE links SET checked='0' WHERE linkfrom='$linkfrom'";      
mysql_query ($query) or die ('Could not query.');

Link to comment
https://forums.phpfreaks.com/topic/183021-return-link-checking/#findComment-966046
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.