Welcome Guest, Not a member yet? Register   Sign In
how to check if url exists
#1

(This post was last modified: 04-14-2025, 10:27 PM by InsiteFX.)

how to check if URL exists with codeigniter ?

I didn't find and try to do it with simple php but it is not as easy as I read. Maybe because php change and what works 10 years ago does not work today in 2025 with php 8 (I forget the decimal)
I try
curl_init($url))
  $headers = get_headers($url);
and differents easy solution but when I really check it it accept that it should not or I got php error message 
Do codeigniter have already done solution ?

I even try  if ( file_get_contents($url, 'FILE_TEXT', NULL, 0, 1))

But I got this kind of error  file_get_contents(): php_network_getaddresses: getaddrinfo for aaaaaaa failed: Temporary failure in name resolution
instead of true false
I try catch but always have erors  impossible to say if I have any kind of error it is false
CodeIgniter 4.6
Reply
#2

Are you checking if a url exists or if a file exists?
Reply
#3

lets re-phrase the question.

if someone enters a URL like : domain.com/elephant

does that URL cause an event like loading a view or does something else; id that what you mean ?

for a view you can do something in a controller like

Code:
public function showView($page)
{
    
    

$this->logic1= file_exists(APPPATH."Views/".$page.".php");

if( $this->logic1 == 1 )
{
load view
}
else

{

load view called info which states , page poes not exits
}



rout to pick up is :


Code:
$routes->get('(:any)','Pages::showView/$1');
//where Pages is a controller class and showView a method of the class using code above
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#4

How to check the existence of URL in PHP?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB