Jump to content

Validating a link


tcorbeil

Recommended Posts

The code isn't working.. keep getting errors..

 

Because I'm new to PHP, on a quick observation, I see on '(' at the start of the if but none at the end..  I added one at the end of the expression but still got an error.. then I took both out completly..  still no go..

 

Any advise?

 

T

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213580
Share on other sites

Sorry i forgot the second parameter bracket for the file_get_contents(function)

 

It should be:

 

if (!file_get_contents($string)){

echo("Url isn't valid.");

}

 

When you run this, what error do you receive?

 

EDIT:

 

If it's the case that your php version doesn't support file_get_contents, then you could equally try file() which predates it, the only difference being that it doesn't put the entire contents into a string but an array, but that shouldn't matter. I'd suggest after you test that it's working you get rid of the "file not found errors" in the event that an invalid url is entered, just by using an @.

 

 

 

<?php

if (!@file($_REQUEST)){

echo("Invalid URL.");

$error=1;

}

 

if ($error!=1){

// store data

}

?>

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213588
Share on other sites

Ok..

 

When the link is valid, it works...  when the link is invalid, i get :

 

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/tcorbeil/public_html/Global/posting.php on line 12

 

Warning: file_get_contents(http://www.mlbxxx.com) [function.file-get-contents]: failed to open stream: Success in /home/tcorbeil/public_html/Global/posting.php on line 12

Url isn't valid.

 

Posting.php is the file that holds the script you gave me..

 

tried it wil mlb.com and then mlbxxx.com..

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213591
Share on other sites

Hey again Hell Toupee..  that worked! Thank you.. lastly, if the link is invalid, instead of continuing on with the script, can i redirect back to the page such as?:

 

 

if (!file_get_contents($weblink)){

@header("Location: ".$_SERVER['HTTP_REFERER']);

}

 

i tried it but i just stays at a blank page... doesn't seem to redirect..

 

I have that same redirect command at the end of the script if the URL is valid and I know it works..

 

Any advise?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213596
Share on other sites

Sure. WHat is odd is that it always redirects now.. but it is always the last redirect command doing it..  so if the link is bad, it still adds it to my database..

 

<?

 

$username="xxxxx";

$password="xxxxx";

$database="tcorbeil_Links";

$name=$_POST['name'];

$weblink=$_POST['weblink'];

$email=$_POST['email'];

$rss=$_POST['rss'];

$Page = $_POST['Page'];

 

if (!@file_get_contents($weblink)){

@header("Location: ".$_SERVER['HTTP_REFERER']); // do not proceed any further if link is bad....

}

 

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

 

$query = "INSERT INTO Entertainment VALUES ('','$Page','$weblink','$rss', '$name', '$email')";

mysql_query($query);

 

mysql_close();

 

@header("Location: ".$_SERVER['HTTP_REFERER']); // go back to main page after link is stored in database...

 

?>

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213609
Share on other sites

There's obviously some problem about including header twice in a page, even if it is within a conditional statement the other header must be included as the contrary to that if statement. Basically you need to include the actual adding of the database after an else or elseif.

 

This should work:

 

<?

$username="xxxxx";
$password="xxxxx";
$database="tcorbeil_Links";
$name=$_POST['name'];
$weblink=$_POST['weblink'];
$email=$_POST['email'];
$rss=$_POST['rss'];
$Page = $_POST['Page'];

if (!@file_get_contents($weblink)){
@header("Location: ".$_SERVER['HTTP_REFERER']); // do not proceed any further if link is bad....
}
else{

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO Entertainment VALUES ('','$Page','$weblink','$rss', '$name', '$email')";
mysql_query($query);

mysql_close();

@header("Location: ".$_SERVER['HTTP_REFERER']); // go back to main page after link is stored in database...
}

?>

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213624
Share on other sites

Why did you add the @ sign? try this:

 

$file = file_get_contents("http://www.google.com");

print $file;

 

If you get the function not defined error thy using this function:

 


$file = file_get_contents2("http://www.google.com");

print $file;

function file_get_contents2($url){
$url_parsed = parse_url($url);
$fd = fsockopen($url_parsed[host], 80);
$data = ''; $header = false;
fputs($fd,"GET ".$url_parsed[path]." HTTP/1.0\r\n");
fputs($fd,"Host: ".$url_parsed[host]."\r\n");
fputs($fd,"Connection: close\r\n\r\n");
while($line = fgets($fd)) {
if($header) $data .= $line;
if(strlen($line) <= 2) $header = true;
}
fclose($fd);
return $data;
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213627
Share on other sites

The problem isn't the file_get_contents function not working, frost, its that the header redirection afterwards which stops the stuff still being added to the database wasn't working. But I think i fixed that now.

 

The @ was added because if the file isn't found (an invalid url) messy php warnings show up.

 

 

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213632
Share on other sites

Hey again fellas.

 

you were right Hell Toupee, i had to include the else for the remainder of the code and it works great, my hat's off to you Sir!

 

Frost, I have kept your code under close advisement for future use.. as my understanding of PHP improves, it will serve me I'm sure, thank you!

 

T.

Link to comment
https://forums.phpfreaks.com/topic/43979-validating-a-link/#findComment-213635
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.