PHP Dynamic page css error

I have a dynamic php webpage

localhost/ctd/index.php

      

All that work are good, except I add a forward slash to it like below:

localhost/ctd/index.php/

      

css doesn't load.

is it a css or PHP error?

The complete index.php code

<?php
define('pageaccess', TRUE); 
$action = $_GET['p'];
include "views/header.php";
if($action=='student')
{
    include "views/student.php";
}else if($action=='college'){       
    include "views/college.php";
}else if($action==''){      
    include "views/home.php";
}else{
    include "views/404.php";
}   
include "views/footer.php";
?>

      

CSS link

<link rel="stylesheet" type="text/css" href="css/ctdu_style.css" />

      

Point me in the right direction.

thank

+3


source to share


2 answers


The problem is your link is relative.

For example, if you have a link like this:

<link rel="stylesheet" href="css.css">

      

And you are in now http://localhost/example.php/

, then download http://localhost/example.php/css.css

. It depends on the server, some servers implement this "feature" and some don't. The browser thinks that this is a directory, but in fact it is not. Some PHP scripts use this behavior, for example MediaWiki uses it index.php

as a directory for emulation .htaccess

when the server does not support files .htaccess

but does support CGI scripts as directories.



The solution would be to either ensure that each path uses the root of the page instead of the current directory (e.g. /css.css

instead of css.css

).

The second solution will be kind of hacky but will convert links. Don't use if you really want to use links like this (but I doubt it: P).

<?php
$request_uri = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
if ($request_uri !== $_SERVER['SCRIPT_NAME']) {
    header('Location: ' . $_SERVER['SCRIPT_NAME']);
    exit;
}

      

+2


source


I ran into this problem a while ago, but the problem was that I was using .htaccess to rewrite the urls and when added at the end of the url /

it called the css and other included files to return a 404 Not Found error.

The reason is that if you have one /

at the end, it will interpret index.php

as a directory, not a file.

The fix that worked for me and may work for you is to prefix all your included files with /

, for example



<link rel="stylesheet" type="text/css" href="css/ctdu_style.css" />

      

will become

<link rel="stylesheet" type="text/css" href="/css/ctdu_style.css" />

      

+1


source







All Articles