Solved

url_exists returns false for existing url

Posted on 2009-05-20
8
629 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

930 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

8 Experts available now in Live!

Get 1:1 Help Now