Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

url_exists returns false for existing url

Posted on 2009-05-20
8
Medium Priority
?
638 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 2000 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses

916 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