baseurl in php

How to Create a Base URL in PHP?


In this blog, we will learn how to create a Base URL in PHP.

Creating a Base URL in PHP is explained below step by step

Step 1: baseurl.php

Create a baseurl.php file

This file defines the base URL for your website and extracts the current page’s information. Here’s a breakdown of the code:

$url = basename($_SERVER['REQUEST_URI']);

This line retrieves the current URL and extracts the last part of it, which represents the current page.

$base_url = "http://localhost/blog/base-url-demo/";

Note: This is my path location

This line sets the base URL for your website. You might need to change this URL to match the actual URL of your website.

$page =  basename($_SERVER['PHP_SELF']);
$pagename = ucwords(str_replace(".","",str_replace("_", " ", $page)));

These lines retrieve the current PHP script’s filename and remove underscores and periods from it. It then capitalizes the words and sets the result as the page name.

if($pagename == "Index"){
   $pagename = "Home";
}else{
   $pagename = $pagename;
}

This block of code checks if the page name is “Index” and changes it to “Home” if that’s the case.

Full code:

<?php 

	$url = basename($_SERVER['REQUEST_URI']);

	$base_url = "http://localhost/blog/base-url-demo/";	 

	$page =  basename($_SERVER['PHP_SELF']);
	$pagename = ucwords(str_replace(".","",str_replace("_", " ", $page)));

	if($pagename == "Index"){
	   $pagename = "Home";
	}else{
	   $pagename = $pagename;
	}
	
?>

Step 2: index.php

Create an index.php file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

</head>
<body>


<?php
include_once 'baseurl.php';
include_once 'header.php';
?>

    <h1>Home</h1>
    <h3><?php echo $base_url; ?></h3>

    
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

This is your website’s home page. It includes the base URL definition and header. The page content consists of an “<h1>” tag and an “<h3>” tag displaying the base URL.

Step 3: header.php file

Create a header.php file

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="<?php echo $base_url; ?>">Base URL</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item">
      <a href="<?php echo $base_url; ?>index.php" class="nav-link">Home</a>
      </li>
      <li class="nav-item">
      <a href="<?php echo $base_url; ?>about.php" class="nav-link">About</a>
      </li>
      <li class="nav-item">
      <a href="<?php echo $base_url; ?>contact.php" class="nav-link">Contact</a>
      </li>
      <li class="nav-item">
      <a href="<?php echo $base_url; ?>services.php" class="nav-link">Services</a>
      </li>
    </ul>
  </div>
</nav>

This file contains the navigation bar, which includes links to different pages of your website. The links are created using the “$base_url” variable combined with the page names. The navigation bar is defined using Bootstrap’s navbar component.

Step 4: about.php

Create an about.php file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

</head>
<body>
<?php
include_once 'baseurl.php';
include_once 'header.php';
?>
    <h1>About Us</h1>
    <h3><?php echo $base_url; ?></h3>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

This is a sample page that follows a structure similar to index.php. It includes the base URL definition, and header, and displays a <h1> tag and a <h3> tag displaying the base URL.

Summary:

Here’s an example of how to build a basic website structure using PHP. The baseurl.php file defines a base URL for the website, which is used throughout the site to generate links. The navigation bar in the header.php file displays links to different pages using this base URL. The actual page content is displayed in Index.php and about.php.

Download Source Code:


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *