3

i have a dynamic php web page

localhost/ctd/index.php

All working good except if i add a forward slash to it like below:

localhost/ctd/index.php/

the css doesnt load..

is this a css bug or PHP?

The full code of index.php

<?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 to the right direction.

Thanks

4
  • I have seen this kind of issue in so many php enabled web applications. I believe we have a fix for it. Commented Mar 19, 2012 at 15:34
  • 1
    This is most likely caused by relative path issues in the CSS includes, but we cannot know for sure without seeing your code. Can you post the relevant snippets? Commented Mar 19, 2012 at 15:37
  • 2
    If you use a relative path for your css, it should search that files in the directory "localhost/ctd/index.php/". I think that directory not exists ;) You can use Tamper Data for showing which files were loaded (addons.mozilla.org/en-US/firefox/addon/tamper-data) Otherwise use absolute paths for your css! Commented Mar 19, 2012 at 15:38
  • Show your <link> tag that loads the css, and check your server's access/error logs to see if the css file is being requested at all, and what the requested url is if it is. Commented Mar 19, 2012 at 15:39

2 Answers 2

2

The problem is that your link is relative.

For example, if you have link like this:

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

And you're currently at http://localhost/example.php/ then it loads http://localhost/example.php/css.css. It's server-specific thing, some servers implement this "feature", some not. The browser thinks it's directory, while in fact it isn't. Some PHP scripts use this behavior, for example MediaWiki uses index.php as directory to emulate .htaccess when server doesn't support .htaccess files, but supports CGI scripts as directories.

The solution would be either making sure that every path uses page root instead of current directory (for example /css.css instead of css.css).

Second solution would be sort of hacky, but would convert links. Don't use if you actually want to use this sort of links (but I doubt this :P).

<?php
$request_uri = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
if ($request_uri !== $_SERVER['SCRIPT_NAME']) {
    header('Location: ' . $_SERVER['SCRIPT_NAME']);
    exit;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I came across this issue myself a while ago, but my issue was more the fact I was using .htaccess to rewrite urls and when a / was added at the end of a url it caused the css and other included files to return a 404 Not found error.

The reason is that if you have a / at the end it will interpret index.php as being a directory rather than a file.

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

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

would become

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.