• 43

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /var/www/html/cnasolution/site/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/site/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/site/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to Get An URL of is_single in is_page [duplicate]

How do I check if a URL exists (not 404) in PHP?

Download script demo [LINK] [Origin]
Download script demo [LINK 2] [Onedrive] Download script demo [LINK 2] [Google drive]

get_headers() returns an array with the headers sent by the server in response to a HTTP request.

$image_path = 'https://your-domain.com/assets/img/image.jpg';  $file_headers = @get_headers($image_path); //Prints the response out in an array //print_r($file_headers);   if($file_headers[0] == 'HTTP/1.1 404 Not Found'){    echo 'Failed because path does not exist.
'; }else{ echo 'It works. Your good to go!
'; }
  • 7
Reply Report

cURL can return HTTP code I don’t think all that extra code is necessary?

function urlExists($url=NULL)     {         if($url == NULL) return false;         $ch = curl_init($url);         curl_setopt($ch, CURLOPT_TIMEOUT, 5);         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         $data = curl_exec($ch);         $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);         curl_close($ch);          if($httpcode>=200 && $httpcode<300){             return true;         } else {             return false;         }     } 
  • 40
Reply Report

One thing to take into consideration when you check the header for a 404 is the case where a site does not generate a 404 immediately.

A lot of sites check whether a page exists or not in the PHP/ASP (et cetera) source and forward you to a 404 page. In those cases the header is basically extended by the header of the 404 that is generated. In those cases the 404 error not in the first line of the header, but the tenth.

$array = get_headers($url); $string = $array[0]; print_r($string) // would generate:  Array (  [0] => HTTP/1.0 301 Moved Permanently  [1] => Date: Fri, 09 Nov 2018 16:12:29 GMT  [2] => Server: Apache/2.4.34 (FreeBSD) LibreSSL/2.7.4 PHP/7.0.31  [3] => X-Powered-By: PHP/7.0.31  [4] => Set-Cookie: landing=%2Freed-diffuser-fig-pudding-50; path=/; HttpOnly  [5] => Location: /reed-diffuser-fig-pudding-50/  [6] => Content-Length: 0  [7] => Connection: close  [8] => Content-Type: text/html; charset=utf-8  [9] => HTTP/1.0 404 Not Found  [10] => Date: Fri, 09 Nov 2018 16:12:29 GMT  [11] => Server: Apache/2.4.34 (FreeBSD) LibreSSL/2.7.4 PHP/7.0.31  [12] => X-Powered-By: PHP/7.0.31  [13] => Set-Cookie: landing=%2Freed-diffuser-fig-pudding-50%2F; path=/; HttpOnly  [14] => Connection: close  [15] => Content-Type: text/html; charset=utf-8  )  
  • 3
Reply Report
  • 17

I run some tests to see if links on my site are valid - alerts me to when third parties change their links. I was having an issue with a site that had a poorly configured certificate that meant that php's get_headers didn't work.

SO, I read that curl was faster and decided to give that a go. then i had an issue with linkedin which gave me a 999 error, which turned out to be a user agent issue.

I didn't care if the certificate was not valid for this test, and i didn't care if the response was a re-direct.

Then I figured use get_headers anyway if curl was failing....

Give it a go....

/**  * returns true/false if the $url is present.  *  * @param string $url assumes this is a valid url.  *  * @return bool  */ private function url_exists (string $url): bool {   $ch = curl_init($url);   curl_setopt($ch, CURLOPT_URL, $url);   curl_setopt($ch, CURLOPT_NOBODY, TRUE);             // this does a head request to make it faster.   curl_setopt($ch, CURLOPT_HEADER, TRUE);             // just the headers   curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, FALSE);  // turn off that pesky ssl stuff - some sys admins can't get it right.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);   // set a real user agent to stop linkedin getting upset.   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');   curl_exec($ch);   $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   if (($http_code >= HTTP_OK && $http_code < HTTP_BAD_REQUEST) || $http_code === 999)   {     curl_close($ch);     return TRUE;   }   $error = curl_error($ch); // used for debugging.   curl_close($ch);   // just try the get_headers - it might work!   stream_context_set_default(array('http' => array('method' => 'HEAD')));   $file_headers = @get_headers($url);   if ($file_headers)   {     $response_code = substr($file_headers[0], 9, 3);     return $response_code >= 200 && $response_code < 400;   }   return FALSE; } 
  • 17
Reply Report