Solved

url_exists returns false for existing url

Posted on 2009-05-20
8
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 16

Expert Comment

by:jessc7
ID: 24434288
Screenshot of validation results.
new1.png
0
 
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
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 dynamically set the form action using jQuery.

738 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