Php does not load Css

Asked

Viewed 2,954 times

0

Good people, I am making a very simple page, to return to the programming.

Here’s the problem: my php is not loading Css, Img or Js.

Yes, I’ve tried to put the absolute/relative path

My directories:

  1. test/
    • public/
      • css
      • js
    • src/
      • header.php
    • index php.


I happen to run there "php -ss localhost:8000 index.php" and try to access the website, it will load normal page, but without any css and without any js.

in my "index.php" occurs a include of "header.php", including css is inserted through html tag:

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

I’ve tried too:

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

also tried ridiculous things like put any path to see if it gave:

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

I’ve tried the relative too:

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

I tried several things, the relative path also does not work... Anyway, I researched for a couple of days on the Internet and nothing. In every forum is said to use the absolute path, but I’ve used and nothing.

In the terminal, this message appears:

PHP 7.0.19-1+deb.sury.org~xenial+1 Development Server Started at Sat May 13 17:14:49 2017

[Sat May 13 16:49:06 2017] 127.0.0.1:51976 Invalid request (Unexpected EOF)

I’ve reinstalled php, apache2 and everything

Use the Ubuntu 16.04 LTS


It worked with a giant gambiarra here guys...

I did something with slim, to try to put a friendly URL, then I did a single route for a css (As if it was a url for a page, but for . css).

Hence, for example, the "/meucss" route renders the file "meucss.css" as if it were a page.

In the HTML tag to pull the css from there I put only href="/meucss" and it loads the css... But do not apply Stilo, just press

However, there are no conditions, having to put routes for each css/img/js I insert on the page... Someone has a tip?

Valeeu!

  • An error appears in the browser console?

  • Only the 404, which is natural because it is not finding the file...

  • Yeah, I could put the whole log in?

  • Look, it worked here (don’t ask me why) when instead of "php -ss localhost:8000 index.php" I just used "php -S localhost:8000"...

  • You’re right, in fact the sS in capital letters does not exist according to doc: http://php.net/manualen/features.commandline.options.php, strange that he executed even so

  • But if I run "php -S localhost:8000 index.php" it doesn’t work... To work I have to omit "index.php"

  • Mathues see if you can understand the explanation

Show 2 more comments

1 answer

1


On the built-in server when you do this

  php -S localhost:8000 index.php

In fact index.php will work as a "proxy", that is, you should not actually pass the index.php, you should only pass something if you want to handle the requests, for example simulate an Apache mod_rewrite.

Imagine you have a system that creates dynamic CSV generated by a PHP, but you want the URL to really look like a . csv, something like http://site/download/2015-04-03.csv, on an Apache server . htaccess could look something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/download/([a-z0-9\-]+)\.csv$ csv.php?date=$1 [NC]

But the PHP built in server, does not support this, so we have to write a php that does this, for example rewrite.php:

<?php
$requri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$pathinfo = urldecode($requri);

//Se o arquivo existir então não usa o rewrite
if (is_file($pathinfo)) {
    return false;
}

//Testa com regex a rota, se falha envia false e o PHP procura um arquivo de verdade ou emite erro 404
if (!preg_match('#^/download/([a-z0-9\-]+)\.csv#', $pathinfo, $match)) {
     return false;
}

//Se o preg_match fizer o "match" então simula a passagem do _GET e chama o csv.php
$_GET['date'] = $match[1];

require 'csv.php';

So in your case the problem is that you were using index.php as a proxy, like the one I did above and because you didn’t have the return false in the proxy and was still being processed HTML, so when you accessed http://localhost/meu.css in fact it pointed to the index.php even if the URL was different.

php built in server already does the whole service of a server, it is not necessary to define any initial file unless you really want to control/manipulate HTTP requests and responses.

The only thing you should point out is if you’re going to create a .bat or a .sh and end up running them from another directory that is different from the directory where yours is located index.php, think that the content of iniciarservidor.sh be something like (whether it’s Linux or Mac):

#!/bin/bash
php -S localhost:8000

And then suppose you have this:

/home/matheus/projeto
       |       |
       |       +-------- index.php
       |
       +---------------- iniciarservidor.sh

Call the iniciarservidor.sh the server will search for the files from the folder matheus and not the folder projeto, then you may prefer to point to the default directory like this:

#!/bin/bash
php -S localhost:8000 -t /home/matheus/projeto

All right, this way it doesn’t matter where you call the iniciarservidor.sh it will always start the server considering the folder /home/matheus/projeto as the "root" of your server.

  • 1

    Putz bro, thanks anyway, thanks for the attention and the time to explain me! I understood the stop... Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.