tcorbeil Posted March 23, 2007 Share Posted March 23, 2007 and yet another.. You guys are awesome BTW!! If I have a customer input in a string: $String = "oilers.com"; What I'm doing is storing the links but I want to vaildate it first.. How would I verify the $String is a valid link? T. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 cURL would work or file_get_contents would also work. I think cURL may have more options that will suit your needs. http://us3.php.net/cURL Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 Hey Frost. Thanks for your response.. Is there not a way to ping a link in a simple way? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 If you have PEAR than there is this: http://pear.php.net/manual/en/package.networking.net-ping.ping.php http://us3.php.net/manual/en/function.checkdnsrr.php See what happens with those. Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 Very simplistic but ye get the basic idea: if (!file_get_contents($string){ echo("Url isn't valid."); } On other hand, if you're looking for a ridiculously indepth pinging class: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786 Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 I'll try that script Hell Toupee, thanks.. All i need is a basic, ping or check to see if the url is there, return result.. I'll let you know. T. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 Frost, i checked with the server info regarding PHP version installed and didn't see anything along the lines of PEAR.. I do know that CURL is installed... i hope i can avoid a bunch of script but if i need to, i'll look into it.., Thank you. T. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 Try toupee's, I didn't realize file_get_contents returned false if it was not successful. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 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 } ?> Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 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.. Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 Sorry didn't see your reply there before I edited, make it !@file_get_contents($string). Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 Can you post the full script so I can see the context the working redirection works in? Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 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... ?> Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 Hmm It works for me, take out all the @'s and see if you get any error messages. Change the first redirect line to to "exit;" just to test that if the url isn't valid that the script is still picking up on it. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 Ok. I tried putting in the exit; instead of first redirect.. it works.. program stops.. I took the @symbol off of (I'm assuming you meant just the redirect commands so thats what I did) and it still was redirecting from the last redirect.. very odd... Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 Hell Toupee, i tried this just in case it was anything to do with my main page.. if (!@file_get_contents($weblink)){ // exit; header("Location: "."http://www.google.ca"); } and it still didn't work... Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 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... } ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 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; } Quote Link to comment Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment 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.