Solved

url_exists returns false for existing url

Posted on 2009-05-20
8
635 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secureā€¦
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 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