Hi, my name is
Lee Harris and
I'm a Freelance Web Designer

PHP conditional – check url has https or http

26.Sep.2022 | , ,

You can use the following method in php to create a conditional that checks if a url has http or https.

Using parse_url() on a url string creates an array.

$url = parse_url($your_url);

You can then use $url[‘scheme’] in an if statement to check for http or https.

Full code below. Update the $your_url variable to your required url source.

<?php 

$your_url = 'https://www.website.co.uk';
$url = parse_url($your_url);

if ( $url['scheme'] == 'https' ) : 
   // Do something if url has https...
endif;

?>

,

  • html5
  • css3
  • sass
  • php
  • WordPress
  • WooCommerce