Check if link exists - php

I have a phpBB forum on my localhost for learning purposes..now I'm trying to do this using PHP :
When a link gets posted,a script checks if the exact link exists and if it does then the process of posting the message doesn't continue.
EDIT: This is what I have in includes/message_parser.php
function url_exists($url) {
$handle = #fopen($url, "r");
if ($handle === false){
return false;
fclose($handle);}
else{
return true;
fclose($handle);}}
And this is what I have in posting.php
$your_url = "http://www.somewhere.com/index.php";
$your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);
if (url_exists($your_url))
{
echo 'yeah, its reachable';
}
else
{
echo 'what da hell..';
}
It works. I can see it echoing what da hell when I post a link that exists,but the problem is that the post gets posted. what I want now is,if the link exists,then don't allow the post to be posted.
2ND EDIT:
if($submit){
$URL = "http://www.fileserve.com/file/rP59FZ2";
preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $url);
if(url_exists($url)) {
echo "Link exists!";
}
That's what I did to prevent submitting the topic when the url exists. not working :\

Checking if link return status code 200 (dunno about 30x)
Using cURL:
function url_exists($url) {
$ch = #curl_init($url);
#curl_setopt($ch, CURLOPT_HEADER, TRUE);
#curl_setopt($ch, CURLOPT_NOBODY, TRUE);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', #curl_exec($ch) , $status);
return ($status[1] == 200);
}
// now call the function
$myUrl = "http://www.somewhere.com";
if(url_exists($myUrl)) {
echo "Link exists!";
} else {
echo "Link does not exist :(";
}
Without cURL:
function url_exists($url) {
$h = get_headers($url);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
return ($status[1] == 200);
}
// now call the function
$myUrl = "http://www.somewhere.com";
if(url_exists($myUrl)) {
echo "Link exists!";
} else {
echo "Link does not exist :(";
}
You can play around with it :)
UPDATE
To the updated question.
I don't know exactly how phpBB works or where you're trying to catch it, but some suggestions to stop the posting might be to check for the link using javascript/jQuery and then disable the submit button alerting/printing a statement as to why the post wasn't posted.
I'm not that into regex and such, but you would check the post if it contains any link you where looking for, and then "block" the submit of the form.
Something along the lines of:
$('#myform').submit(function(event){
// disable the submit button so the user doesn't spam it
$('#myform input[type=submit]', this).attr('disabled', 'disabled');
// check with regex for the links
// if it's found return true else return false to variable preventSubmit
if(preventSubmit) {
// prevent the form from being submitted
event.preventDefault();
// notify the user
alert("You cannot post that link!");
return false;
}
});
as extra security, you could disable the submit by default, and only enable it once the javascript has loaded.

try:
$url = 'http://www.anyURL.com';
$hndl = #fopen($url,'r');
if($hndl !== false){
echo 'URL Exists';
}
else
{
echo "URL Doesn't exist";
}

First check to see whether the link format is valid:
function isValidURL($url) {
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
Then make sure the resource that the link refers to actually exists:
function checkLink() {
$handle = fopen("http://www.example.com/", "r");
if ($handle) { fclose($handle); return true; }
return false;
}

Related

file_exists($url) does not work in case of ajax call

I have a file called 'test.php' inside the file i have the following code
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
jQuery.ajax({
url : 'http://example.com/TEST/images.php',
type : 'POST',
data : {
name : 'black-ring1.gif'
},
success : function(data, status) {
if(data){
alert(data);
}
}
});
});
});
</script>
</head>
<body>
<p id="btn">Button</p>
</body>
</html>
My 'images.php' file which executes when ajax called conteains the following code.
<?php
$image_name = $_POST['name'];
$src = "http://example.com/images/icons/" . $image_name;
if(file_exists($src)){
echo "Exists";
}else{
echo "Does not exist";
}
inside the 'images.php' file_exists($url) gives me always wrong answer(Does not exist).. I am sure $url really exits in the given url.. But why it does not work. How i can make it work ... Please help me.. Thanks..
file_exists can't be used with http:// URLs. From the documentation:
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
If you follow that link and then follow the http:// link, it says:
Supports stat() No
Instead, you can simply try to open the URL. If it succeeds then the URL exists.
<?php
$image_name = $_POST['name'];
$src = "http://example.com/images/icons/" . $image_name;
if($fh = fopen($src)){
echo "Exists";
close($fh);
}else{
echo "Does not exist";
}
First you need to validate that you pass the name
(!empty($_POST["name"])) {
$image_name = $_POST['name'];
$src = "http://example.com/images/icons/" . $image_name;
}else{
echo "no, name";
}
then you can use CURL to check if the url exist
if(is_url_exist($src)){
echo "Exists";
}else{
echo "Does not exist";
}
this code is from this SO
function is_url_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code == 200){
$status = true;
}else{
$status = false;
}
curl_close($ch);
return $status;
}
you can also try from this SO
if (getimagesize($src) !== false) {
echo "Exists";
}else{
echo "Does not exist";
}
file_exists() checks files. A URL is not a file. If the image file you're looking for is on your local server, then you can just use the full path name to it:
if (file_exists('/path/to/images/foo.png')) {
...
}
If the file is on a remote server, and you want to test that the URL referencing it is valid, you might use CURL to perform a HEAD request against it:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
$result = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
if ($info['http_code'] == 200) {
echo "image exists\n";
}
I would not recommend doing something like file_get_contents($url) unless you actually need the image contents, because this will actually download the full image and then just throw it away, wasting bandwidth for both you and the remote server.

Add http or https for user input validation

$xml = $_GET['url']
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
..
..
if the user put without http or https my script will be broken, is concatenation a good way to validation in this case?
The simplest way of doing this is checking for the presence of http:// or https:// at the beginning of the string.
if (preg_match('/^http(s)?:\/\//', $xml, $matches) === 1) {
if ($matches[1] === 's') {
// it's https
} else {
// it's http
}
} else {
// there is neither http nor https at the beginning
}
You are using a get method. Or this is done by AJAX, or the user appends a url in the querystring You are not posting a form?
Concatenation isn't going to cut it, when the url is faulty. You need to check for this.
You can put an input with placeholder on the page, to "force" the user to use http://. This should be the way to go in HTML5.
<input type="text" pattern="^(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$" placeholder="http://" title="URLs need to be proceeded by http:// or https://" >
This should check and forgive some errors. If an url isn't up to spec this will return an error, as it should. The user should revise his url.
$xml = $_GET['url']
$xmlDoc = new DOMDocument();
if (!preg_match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, $xml ) )
{
echo 'This url is not valid.';
exit;
}
else if (!preg_match(/^http(s)?:\/\/, $xml))
{
//no http present
$orgUrl = $xml;
$xml = "http://".$orgUrl;
//extended to cope with https://
$loaded = loadXML();
if (substr($loaded, 0, 5) == "false")
{
//this attempt failed.
$xml = "https://".$orgUrl;
$loaded = loadXML();
if (substr($loaded, 0, 5) == "false")
{
echo substr($loaded, 6);
exit;
}
}
}
else
{
$loaded = loadXML();
}
function loadXML()
{
try {
return $xmlDoc->load($xml);
}
catch($Ex)
{
return echo 'false Your url could\'t be retrieved. Are you sure you\'ve entered it correctly?';
}
}
You can also use curl to check the url before loading xml:
$ch = curl_init($xml);
// Send request
curl_exec($ch);
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "$error_message :: while loading url";
}
// Close the handle
curl_close($ch);
Important side-note: Using this methods to check if the url is available and than take the appropriate action can take a very long time, since the server response can take a while to return.

Link PHP file as PNG image? - Image is a variable based on server status

Here is my code, you can view an example of it by going to:
www.craftquake.com/statusChecker.php?site=MCnet
<?php
$getter = $_GET['site'];
if ($getter == 'ts3')
{ $site = test_port('ts3.craftquake.com',10011,4); }
if ($getter == 'MCquake')
{ $site = test_port('play.craftquake.com',25565,4); }
if ($getter == 'MCnet')
{ $site = test_port('minecraft.net',80,4); }
$teamspeak = test_port('ts3.craftquake.com',10011,4);
$online = '<img src="/online.png">';
$offline = '<img src="/offline.png">';
$unknown = '<span class="status-unknown" id="status-image">Unknown</span>';
function test_port($host,$port=80,$timeout=1)
{
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if ( ! $fsock )
{
return FALSE;
}
else
{
return TRUE;
}
}
?>
##HEADER & CSS, ETC
<?php
if ($site == 1)
{ $status = $online;
} else if ($site == 0) {
$status = $offline;
} else {
$status = $unknown;
}
header('content-type: image/png');
readfile($status);
echo $status;
?>
I want to, in the footer of my page, link to this page to display the status. I was doing this with another site's script by linking their status of Minecraft.net's servers as the and it worked perfectly, however I have no idea how they made that work. The images are PNG's, but if there is only one format that works, I can convert them.
I have tried the header(blablabla) function, but it doesn't seem to work...
Thank you very much!
Your variables contain HTML instead of the path name to the image files:
$online = '<img src="/online.png">';
should be:
$online = 'online.png';
Create a unknown status image and put it in $unknown too.
An image should be a seperate request (so, put an <img src="/yourimagescript.php"> in your html page, and in that seperate script output only the image, no html. You could embed (small) images with the data: protocol, but I strongly advise against it.

php/jquery - check if image in url really exists

I found this nifty way to check if a page really exists:
$headers=#get_headers($imageurl);
if(strpos($headers[0],'200')===false){
echo "not valid";
exit;
}
What I need to achieve, though, is to check whether on that page is really only an image.
For example, when you try the following link:
www.google.com/image.jpg
it will return 200 because Google has it's own error page - however, it should be possible to find out not only that there is no image on that page, but also that an image is not the only thing on that page, even when there is other content (like here: http://www.kapstadt-entdecken.de/wp-content/gallery/robben-island/1robben-island.jpg).
How I can I achieve that?
Thanks a lot!
Dennis
You will probably want to use HEAD requests when getting headers (it doesn't do that by default):
stream_context_set_default(array(
'http' => array(
'method' => 'HEAD'
)
));
Second, you can pass a second parameter to get_headers to parse it for you:
$headers = get_headers($imageurl, 1);
Then, you can check the rest as per normal:
if (strpos($headers[0], '200') === false) {
echo "not valid";
exit;
}
if (isset($headers['Content-Type']) && 0 === strncmp($headers['Content-Type'], 'image/', 6)) {
echo "valid image";
} else {
echo "probably not an image";
}
also with get_headers ... one of them will be
image/png
image/jpeg
image/gif
so
$isImage = false;
foreach($headers as $header){
if(strpos($header,'image/')==true){
$isImage = true;
}
}
$headers = #get_headers($imageurl);
$is_image = false;
foreach ($headers as $header) {
if (strpos($header, 'Content-Type: image/') === 0) {
$is_image = true;
break;
}
}

Link checker - mail for invalid links [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I got this link checker script and i would like it to give me a mail when a link doesnt work.
I need it to remember that it send me an email about a link so i dont get multiple emails about the same link.
I would appeciate if anyone help me with this since it was too hard for me.
<?
function checklink($links) {
$working = 0;
$link = $links;
$links = preg_replace('/\s+/', '', $links);
if(strstr($links,"rapidshare.com")){
$var = file_get_contents($links);
if(strpos($var,"You want to download the file ")) {
$working = 1;
}
}
elseif (strstr($links,"megaupload.com")) {
$var1 = file_get_contents($links);
if(strpos($var1,"Please enter")) {
$working = 1;
}
}
elseif (strstr($links,"megashares.com")) {
$var2 = file_get_contents($links);
if(strpos($var2,"Filename:")) {
$working = 1;
}
}
elseif (strstr($links,"sendspace.com")) {
$var3 = file_get_contents($links);
if(strpos($var3,"404 Page Not Found")) {
$working = 0;
}
elseif(strpos($var3,"Sorry, the file you requested is not available.")){
$working = 0;
}
else {
$working = 1;
}
}
elseif(strstr($links,"rapidshare.de")) {
$var5 = file_get_contents($links);
if(strpos($var,"You want to download the file ")){
$working = 1;
}
}
elseif (strstr($links,"mediafire.com")) {
$var4 = file_get_contents($links);
if(strpos($var4,"Sharing")) {
$working = 1;
}
}
if ($working == 1) {
echo "". $link . "";
}
else {
echo "The link is not working. Please let me know about it and I'll fix it.";
}
}
?>
I think the best way would be to collect the links and store them in a database table.
Then have a system that goes through the links and checks, if it works it marks it as a working link and if it doesn't then it marks it as a broken link and sends you an email.
You would then have to do a check to see if a link is in a database (since you cant use mysql's varchar as a unique, because it's maxed out at 255 and links can be longer)
If it is in the database, then check what the result of the scan was.
BTW your way of using file_get_contents is a slow process. Since it downloads the entire page. I would recommend using cURL.
I agree with Olafur, but alternatively if you haven't got access to a database you could use the server's file system to save the stats of an URL in a combined config/log file, like a comma-delimited file. Let's say you have a file like this:
rapidshare.com,You want to download the file,0,0
megaupload.com,Please enter,0,0
megashares.com,Filename:,0,0
The four fields are 'URL', 'text to check for', 'last check result' and 'has mail been sent'. The code could be something like this:
$file = "myfile.txt";
// open the file
$fh = fopen($filename, "r");
// read the full file contents into a string
$contents = fread($fh, filesize($file));
// close the file
fclose($fh);
// split the string into an array of lines
$lines = split("\n", $contents);
// split each line into its fields for processing
$i = 0;
foreach ($lines as $line) {
$checkarray[$i] = split(",", $line);
$i++;
}
Now you can cycle through the array and do whatever you want, writing back the information including the 'mail sent' status field as you go along. Use $fields[0] for the URL, $fields[1] for the text to check for, and you can read the last status with $fields[2] en the 'mail sent' status with $fields[3].
foreach($checkarray as $fields) {
// insert code to do your checks here
...
// write back the results
$fh = fopen($filename, "w");
fwrite($fh, $fields[0] . "," . $fields[1] . "," . $working . "," . $mailsent . "\n";
fclose($fh);
}
Hope this helps you on your way.
this is the code you want:
function StatusCheck($url)
{
$urlparts=parse_url($url);
$curl=new CCurl($url);
$headers=$curl->execute();
$headers=$curl->close();
$headers=$curl->getHeader();
$headers=split("\r\n",$headers);
$status=$headers[0];
print_r($headers);
if (strpos($status,"HTTP/1.1 200 OK")===FALSE)
{
echo date("d.m.Y H:i:s").$url,': bad'."\n";
return 0;
}
else
{
echo date("d.m.Y H:i:s").$url,': good'."\n";
return 1;
}
}
it checks the URL (link) provided and prints out the headers + info if the URL is bad (not working) or good (status 200 OK)
PS: set curl options to follow redirection
EDIT: this is the CCurl class, sorry forgot about it:
class CCurl {
var $m_handle;
var $m_header;
var $m_body;
function CCurl($sUrl) {
$this->m_handle = curl_init();
curl_setopt($this->m_handle, CURLOPT_URL, $sUrl);
curl_setopt($this->m_handle, CURLOPT_HEADER, 1);
curl_setopt($this->m_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->m_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->m_handle, CURLOPT_USERAGENT, "StatusCheckBot 0.1");
return;
}
function getHeader() {
return $this->m_header;
}
function execute() {
$sResponse = curl_exec($this->m_handle);
$this->m_body = substr($sResponse, strpos($sResponse, "\r\n\r\n") + 4);
$this->m_header = substr($sResponse, 0, -strlen($this->m_body));
return $this->m_body;
}
function close() {
curl_close($this->m_handle);
return;
}
}

Categories