Solved

url_exists returns false for existing url

Posted on 2009-05-20
8
628 Views
Last Modified: 2012-08-13
I use the function url_exists() to check whether a url exists (wow). For one set of urls, this doesn't work.

- For the same domain but different urls, the functions works correctly.
- When I download the xml file and save it locally, and run the function using the local url it works.

The headers I get back are as follows:

[0] => HTTP/1.1 200 OK
[1] => Date: Wed, 20 May 2009 15:26:22 GMT
[2] => Server: Apache/2.2.8 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8k
[3] => Last-Modified: Wed, 20 May 2009 15:21:54 GMT
[4] => ETag: "12d00000000006c-2635-46a59965b054e"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 9781
[7] => Connection: close
[8] => Content-Type: application/xml

Any ideas what could cause this problem?
<?php

function url_exists($url) 

{

   $hdrs = get_headers($url);

    return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;

} 

?>

Open in new window

0
Comment
Question by:R7AF
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:jessc7
ID: 24433981
I'm not sure all the escaping is necessary, is it? The following works for me using an online expression checker:

^HTTP\/\d+\.\d+\s+2\d\d\s+.*$

http://www.nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp

Here is an alternative using CURL:
http://www.ajaxapp.com/2009/03/23/to-validate-if-an-url-exists-use-php-curl/
0
 
LVL 16

Expert Comment

by:jessc7
ID: 24434068
Here are two screenshots of the regex. I'm still thinking through why your original works sometimes...
original-fail.png
succeed.png
0
 
LVL 16

Accepted Solution

by:
jessc7 earned 500 total points
ID: 24434275
OK, I just testing your original regex on a test server using preg_match, and it was fine. I'm wondering if you can make your regex slightly less strict?

^HTTP\/\d+\.\d+.+2\d\d

This doesn't get so strict on the use of white space.
0
 
LVL 16

Expert Comment

by:jessc7
ID: 24434288
Screenshot of validation results.
new1.png
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 13

Author Comment

by:R7AF
ID: 24434636
Thank you! I will test this on Friday and let you know if it solved my problem.
0
 
LVL 16

Expert Comment

by:jessc7
ID: 24529800
Hi R7AF, were you able to complete further testing on this issue?
0
 
LVL 13

Author Comment

by:R7AF
ID: 24572264
Yes, and the issue still exists. I'll try to update this question soon.
0
 
LVL 13

Author Closing Comment

by:R7AF
ID: 31583560
Thanks for your input. I'm closing this question now. Maybe I'll look into it in the future again, and will post another question about it then.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now