images and css not loading on local wamp server using PHP - php

I have ran into a problem where by using /js/filename.js or /images/image.jpg etc is not loading anything on my WAMP Server. What it seems like is that its not getting the base url properly here. Screenshots will give you better idea.
Trying to reference the files with "/" in the start but it fails to load anything. I need to have it because i would be working with URL Rewrites so in that case it should load the files from there. Please let me know about any wamp server setting that is causing this issue.
Regards.

You are referring to your files with a root-path like "/my-file.txt" (i.e. having a slash at begin),
which would result to "C:/wamp/www/my-file.txt" being requested.
Change all your paths to be relative paths, for example to "./my-file.txt" path (i.e. with both dot and slash ./),
That would change the file request to something like "C:/wamp/www/my-project/public/my-file.txt" (if your HTML file is in public directory that is).

Actually I would not recommend you using relative paths like ./myfile.txt or something else, as it can get somewhat messy when you try MVC in your code.
Instead I would recommend you set up a virtual host on your wamp machne.
Here you can read more about setting up virtual host in wamp
https://john-dugan.com/wamp-vhost-setup/

Related

How do I get PHPStorm to use the correct path for CSS?

I have been using "Delphi PHP XE2". The file structure has worked fine for over a year on the development machine and the production.
The folders are organised as follows.
src/app_login.php
src/add
src/css
src/js
src/fnc
src/db
src/images
etc.
I am just trying out PHPSTORM. The include files work fine. When I run it in Firefox it can find all images, JavaScript files, etc. But it can not find the CSS files as it is looking for them in
css
rather than
src/css
I haven't changed anything.
Why is the browser now looking at absolute path rather than relative?
How did PHPStorm manage to tell it that?
And how do I coerce it to treat them as relative paths?
I see that in the browser it runs it as:
http://localhost:63342/SRC/app_login.php
instead of
http://localhost:63342/app_login.php
I guess this the problem. How do I coerce it to move down one directory level?
I am trying to set up IIS, I think this might be best for later on. Here are the screen shots:
I configured the hosts file so that when I type wys.com in the browser it tries to run it and shows a blank page. If I type in 192.168.1.0 it just shows a blank page. View Source shows nothing. I was expecting it to run the index.html at d:\wys\src. Why is the browser not running it?
You are using PhpStorm's own simple built-in web server which uses URLs like http://localhost:63342/ProjectName/app_login.php. You cannot make http://localhost:63342/app_login.php using such server as it will not be able to tell what files to serve.
Either use your own Apache/IIS/whatever web server .. or the best you can get with built-in server would be http://ProjectName:63342/app_login.php(IDE needs to know what site/files to serve somehow). For that:
Edit your hosts file (or local DNS server, if preferred and have one) and point ProjectName to your computer's IP (e.g. 127.0.0.1).
Create Deployment entry of correct type (In Place should do), configure it (provide desired URL etc -- http://ProjectName:63342/) and mark it as Default for this project -- now IDE will use URL from there when generating "open in browser" URLs.

Base URL in PHP

I've got a bit of a dilemma, and it's been bothering me for quite some time. I have a local testing server that's set up like so: 127.0.0.1/
My website in offline mode looks like this:
127.0.0.1/websitename/index.php
My live version of the website looks like this:
websitename.com/index.php
I've got the base script for almost all of the links, except for the including header and footer files.
The links in the footer and header files work except for the home page (on the root of the website).
Could anyone redirect me to a proper method of doing multi sub directory base URL linking for both offline and online?
I’ve tinkered around with most of the $_SERVER[] tags and attributes, as well as parse_url().
There's an easy and cool thing you can do in local server called: Virtual host, which allows you to create subdomain to access your local website without entering subdirectories in the url.
Example:
You can do something like the following to access your files:
mysite.localhost/ which is exactly the same as localhost/mysite/index.php
In that way, you don't have to worry about subdirectories when moving you website to the online server.
Links for virtual host:
WAMP
XAMPP
I’ve tinkered around with most of the $_SERVER[] tags and
attributes, as well as parse_url().
Don’t tinker with them. There’s no clean/automated way to do what you are doing. Just set a base path manually in a config file & don’t worry about it—relative paths—ever again. And if you need to set a base URL, the process is similar.
So as far as a file base path goes, you should explicitly set a $BASE_PATH like this:
$BASE_PATH = '/full/path/to/your/codebase/here/';
If you don’t know what your file system base path is, just place this line of code in your PHP code; like index.php:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
Then load that page. Somewhere near the top will be this text:
Your path is: /full/path/to/your/codebase/here/
Then with that set you can change your code to be something like this:
And then set your include_once like this:
include_once $BASE_PATH . 'includes/myfile.php';
Some might say you should use $_SERVER['DOCUMENT_ROOT'] or even dirname(__FILE__) directly with the implication being that you can simplify code portability that way. But the way file paths are set for installs can vary so it just never works well & the chances of you getting snagged on an odd server quirk is high.
It’s always best to just to manually set a $BASE_PATH in a config file when you move code than deal with the headaches caused by PHP constants like $_SERVER not being consistent between installs, setups & configurations.
And as far as a base URL goes, just follow the same thinking with this being on your local development setup:
$BASE_URL = '/websitename/';
And this being on your production server:
$BASE_URL = '/';
So with that $BASE_URL set then you can just do this:
I’ve got the base script for almost all of the links, except for the
including header and footer files.
Now just prepend any path you might need requested via a URL with $BASE_URL & you should be good to go.
I suggest you move to a development environment which more closely reflects the live system. For this, you can run a WAMP server and configure it to serve your web site as a domain like mysite.local and then you simply edit your hosts file so that mysite.local resolves to your 127.0.0.1. Then you just type mysite.local into your browser, it resolves to your local PC, and make sure apache is configured for virtual hosts and listening on port 80.
Your hosts is a local DNS lookup file found in windows\system32\drivers\etc. You may need to open it in Notepad which is run as administrator in order to be able to edit it.

Using /directory/file.js doesn't include the domain

So I've never seen this before. I have an EC2 server (first time setting this up) using Debian Linux and Apache 2.2. Using a path like so /js/file.js is looking for http://js/file.js. On my local machine and my dreamhost shared server I don't have this problem. In fact I've never seen this problem on a server before. What it should do is look for http://domain.com/js/file.js. Does anyone have some idea of why this could be happening? I've poured over my php.ini file and don't have any hint at what I should change or add to fix this.
What kind of syrup did you "pour" over your php.ini file? It may have gummed up the works! ;)
Have you looked in the output HTML via your browser (View > Page Source), and what you're getting is http://js/file.js? Is that src="http://js/file.js" in a tag? Adding http://domain.com/ should be the work of the browser, not the server. Some browsers display URLs with the domain already added, while others show exactly what you sent to the page. Are you sending any tags that maybe are missing the domain? I think it's only supposed to apply to relative URIs, but it's worth checking. Did you actually use js/file.js or /js/file.js? They're very different.
Ok, so I figured it out. The problem was that on my local machine and on my other server I was not at the root domain so I was using $_SERVER['SCRIPT_NAME'].DIRECTORY_SEPARATOR."js/" to determine the root url to make an alias to the js files path. This returned /app/js/. This worked fine when the files where in a sub folder from the domain somewhere e.g. domain.com/app/js/file.js.
However once I was installing this app on my server and it was the root application $_SERVER['SCRIPT_NAME'].DIRECTORY_SEPARATOR."js/" was returning //js/. That was the problem! That is not the same as /js/. That was bypassing the domain as part of the url and instead telling the browser to look at http://js like it was a fully qualified url. I am assuming // is shorthand for http:// though I've never tried this before.
To fix this I hacked together this function based off of something in the Yii Framework, which is what I'm using for this application.
function getBaseUrl() {
$scriptName=basename($_SERVER['SCRIPT_FILENAME']);
if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
$_scriptUrl=$_SERVER['SCRIPT_NAME'];
else if(basename($_SERVER['PHP_SELF'])===$scriptName)
$_scriptUrl=$_SERVER['PHP_SELF'];
else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
$_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
$_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
$_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
else
throw new Exception('The App is unable to determine the entry script URL.'));
return rtrim(dirname($_scriptUrl), '\\/');
}
I hope this helps someone else.

Exclude direct paths on server, i.e. css, js, etc

I am experimenting with Laravel 4, I have successfully installed and did a couple things and that. Unfortunately, I cannot load any CSS into my HTML.
The CSS url is correct, which I used HTML::style('css/bootstrap.css'); to generate for me.
When I access the URL (currently on localhost) http://laravel.dev/css/bootstrap.css, it gives me an error:
Not Found
The requested resource /css/bootstrap.css was not found on this server.
The file is stored in /public/css/bootstrap.css, why can't Laravel avoid this? How would I overcome this issue? It seems that the route is going forward with the application rather than finding the file on the server.
I am running Laravel on my localhost environment, PHP 5.4.0, Mac OS X 10.7.
You probably have a problem with your virtual host or .haccess file, because
HTML::style('css/bootstrap.css');
Should point to
http://laravel.dev/css/bootstrap.css
And it is doing it correctly, but your virtual host configuration for http://laravel.dev/ should point to
/var/www/laravel.dev/public/
So, that file, in fact, is
/var/www/laravel.dev/public/css/bootstrap.css
If you get a file not found on
/css/bootstrap.css
Then I have to assume this is a virtual host or rewrite (.htaccess) configuration problem.
You have to put the assets folders in your root outside public directory according to HTML::style.
Otherwise you can access by http://laravel.dev/public/css/bootstrap.css

what is the difference of absolute path in html and php?

I am developing a website on php, I have installed wamp on my personal computer and my website files are in the www folder of wamp.
now considering www as my root folder i have a template folder in the root folder and header.inc.html file in the template folder. when I try to include this header.inc.html file in any other php file using an absolute path include('/template/header.inc.html'); it gives me error "Failed to open stream: No such file or directory", but when I create a simple html link using the same absolute path it works perfectly and opens the file. below is my test code
<?php
echo 'headerfile';
include('/template/header.inc.html');
?>
if I give the full path for example C:/wamp/www/template/header.inc.html to the include function it works fine.
I am confused that this problem is occurring on my wamp server only and it would work perfectly on any webhost server, or maybe the same problem will exist on a webhost
I would appreciate any help that would clarify my confusion, Thanks.
Absolute paths on the server start from the server's hard disk (C:\).
Absolute paths on the client start from the root of the website (http://example.com/).
You can make use of __DIR__ to make some file on disk relative to the php-file on disk itself:
include(__DIR__.'/template/header.inc.html');
This should solve your issue.
The difference is not that easy to explain because both types of paths - even related - are two pair of shoes. I suggest you start with a very basic HTML website tutorial that explains how to link on your website and where files are located and how that is related to the webserver configuration.
HTML pages live in the client's browser that know nothing about your server's folder structure, and they're relative to the domain name eg. http://example.com/.
PHP programs run on the server side and they deal with the server folders. You shouldn't hardcode full paths in your php programs, because it will cause problems whenever you'll move them between the development server and the live host (just to name an example). Therefore in php files you should either use relative paths to your file, or use the __DIR__ magic constant that gets substituted with the directory where the php file is.
1.) First approach: include('template/header.inc.html');
2.) Second approach: include(__DIR__ .'/template/header.inc.html');
In your case (working on a development machine) both the client and the server is the same box, that might be confusing you.

Categories