when I implement the domain name to my website. some of the other pages and some clickable buttons are not working. Why is this happening? and any solution for this kind of problem? I just implement my domain name which the replace localhost
to www.name.com
then this problem occurred.
-
I think this is a `css` file or `scripts` that are not called, since you need to declare your `base url` which is your domain name. – Roshan Feb 08 '19 at 02:27
-
@Roshan , they are declared on my page tho, these files are already included before i add the domain name of the website – Feb 08 '19 at 02:29
-
@Roshan, but i dont know what `base url` you are talking about sir – Feb 08 '19 at 02:29
-
Can you post here sample of how you call the scripts? And also You can refer [here](https://stackoverflow.com/questions/15481629/how-do-i-set-base-url-for-all-pages-of-my-website) regarding on base url. – Roshan Feb 08 '19 at 02:31
-
@Roshan , okay sir , well i call all the scripts and css files like this `` and css files like this `` – Feb 08 '19 at 02:34
-
I assume you are working with Laravel, right? – M Ansyori Feb 08 '19 at 02:45
-
M Ansyori , just pure php sir. – Feb 08 '19 at 02:46
-
is there any error message from the console you can provide? – M Ansyori Feb 08 '19 at 02:48
1 Answers
Since you have no example of your actual script, one must fly a little blind when answering however, I ran into this same issue when I was first starting out creating sites. I came to a couple conclusions:
1) Create a config file located in the domain root that goes on every top level page that includes reusable items such as domain root path, important folders, session start, maybe database connection credentials, stuff like that. It helps keep your scripts consistent and you always know that you have access to the same things:
/config.php
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT_DIR', __DIR__);
define('FUNCTIONS', ROOT_DIR.DS.'functions');
define('VENDORS', ROOT_DIR.DS.'vendors');
define('BASE_URL', 'https://www.example.com');
# Start session
session_start();
# Create class loader
spl_autoload_register(function($class){
# Turn a class name/namespace into a path
$path = VENDORS.DS.trim(str_replace('\\', DS, $class), DS).DS.'.php';
# Include file
if(is_file($path))
include_once($path);
});
# If you use predominantly functions, create a loader for that (see below)
include_once(FUNCTIONS.DS.'autoload.php');
# Add the the base_url function (as noted by others), since it's going to be used a lot
autoload('base_url');
2) Create a couple handy functions (or classes) including a base url (as mentioned by others):
/functions/base_url.php
<?php
function base_url($path = false)
{
return BASE_URL.$path;
}
If you are using a function-based system, create a function loader:
/functions/autoload.php
<?php
function autoload($func)
{
if(!is_array($func))
$func = [$func];
foreach($func as $function) {
if(!function_exists($function)) {
if(is_file($file = FUNCTIONS.DS.$function.'.php'))
include_once($file);
}
}
}
3) When creating links and domain paths, use the base_url()
:
<a href="<?php echo base_url('/contact/') ?>">Contact</a>
Page html might look something like this:
<?php
include_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
?><!DOCTYPE html>
<html>
<head>
<title>Home page</title>
<link rel="stylesheet" href="<?php echo base_url('/media/css/styles.css') ?>" />
<script src="<?php echo base_url('/media/js/scripts.js') ?>"></script>
</head>
<body>
<nav id="main-menu">
<a href="<?php echo base_url('/') ?>">Home</a>
<a href="<?php echo base_url('/about/') ?>">About</a>
<a href="<?php echo base_url('/contact/') ?>">Contact</a>
</nav>
<img src="<?php echo base_url('/media/images/banner.jpg') ?>" id="banner-image" />
</body>
</html>
Anyway hopefully this is useful.

- 12,498
- 3
- 25
- 33