I am trying to link an action within a form to a php script:
<form action='../includes/RC_APP/process_login.php' method="post" name="login_form">
However it comes back with the error:
The requested URL /includes/RC_APP/process_login.php was not found on this server.
The PHP includes work fine:
<?php
include_once '../includes/RC_APP/db_connect.php';
include_once '../includes/RC_APP/functions.php';
My only thoughts are that the HTML will not process access above the web server document root directory while the PHP will; but this contradicts the security advice on keeping PHP include files outside of the web server's document root.
Any advice would be greatly appreciated.
you need to set base path in the head section.
like this:
<head>
<base href="your_site/sub_directory/">
</head>
The base tag makes everything link differently, including same-page anchor links to the base tag's url instead, e.g:
<a href='?update=1'>A link to this page</a>
becomes
<a href='your_site/sub_directory/?update=1' title='Some title'>A link to the base tag's page instead</a>
if you need to go another folder in same directory in base path e.g a folder includes in the basepath directory link that in this way :
<a href='includes/'>A link to this page</a>
if you need to go another parent folder of base path e.g a folder includes :
<a href='..includes/'>A link to this page</a>
see more details: Is it recommended to use the <base> html tag?
I think I have found the answer to my question, in that a form action cannot access anything above the document root directory:
HTML form action using a php file outside of root directory?
Related
I am a newbie in website development.
here is my problem :
I have 2 HTML files. they are 'index.php' and 'header.php' . I try to include 'header.php' in to the 'index.php' using this code :
'index.php'
<body>
<?php
include("header/header.php");
?>
</body>
'header.php' contain this code :
<h1>Its header</h1>
<img src="img/006-tumblr.png" width="200" height="200">
its the folder hirearchy :
-index.php
--header
--img
-006-tumblr.png
-header.php
When I open 'index.php' , 'header.php' is included but the image is not displaying.
So how can I include 'header.php' with the image?
A good way of proceeding would be to have a folder "img" in your root public HTML path containing all images, eventually with subfolders to separate them. The reason for that is that your main controller is launching from root. In that way, wherever you call your image file, you just have to go to img/ in order to find it. The same logic applies to all media. So, if you intend to have video for example, you could have a main folder media with a img subfolder and a vid subfolder. This type of logic has to be defined at start of project so that you don't have to refactor in the middle of it.
The logic of including a file into another is different from linking a CSS file. When you include a PHP file, the entire code is added to the source file and then the server compiles the codes. So the image files and other resources should be addressed relative to the source document (not the included one). This logic is different in a CSS file and the resource files e.g. a background image is complied relative to the CSS file (Because a CSS may be used in different files hirearchy). So this will work:
<h1>Its header</h1>
<img src="header/img/006-tumblr.png" width="200" height="200"
Footnote: If you want to use header in different files with different hirearchy the solution for the question above is to set BASEURL for your document and setting the resource and anchores relative to the baseurl.
Giving location from root directory makes image accessible from any location.
<?php define('WEBSITE_BASE', $_SERVER["SERVER_NAME"]); ?>
<img src="<?=WEBSITE_BASE?>/header/img/006-tumblr.png" width="200" height="200">
Best linking policy you should always follow to link static assets to your page so that it links wherever the item is used.
On my php site I'm trying to include html pages outside the root folder (the html pages change automatically)
if I include an html page I get the 404 error for the linked files and if I include the linked files too they aren't displayed correctly
path/site/root/index.php
...
include("path/html/index1.html");
include("path/html/index1_files/img.png");
...
path/html/index1.html
...
<img src="index1_files/img.png">
...
result:
You need to specify the directory properly. So, in your case, when linking to the image, replace <img src="index1_files/img.png"> with <img src="./index1_files/img.png">. The ./ goes back into the folder the file is saved in, and ../ goes up one level. Your code is looking for a folder called index1_files within the current page.
I have made my first website and I have a simple contact form that runs a php scripts to send the details inputted in the form to my email. I initially had all the files in the root directory but I am trying to tidy it up so I now have,
all html files in the root directory, I then have folders for php, css, assets, js.
I know that to link to these I need to just do something like,
<link rel="stylesheet" href="css/contactus.css"/>
I believe this tells the html to look in the root directory, then in css folder and for the file contactus.css.
This seems to work for all the css files but not for the php file ( I get a 404 error). I know this file is correct as when all the files where in the root folder it worked fine.
I currently have,
<form action="php/contactform.php" method="post">
I believe my issue is that when using href it will automatically start in the root folder but as I am not using href in this case it is not starting in the root folder. So I just need to find out what I use to link to a different folder to that of the html file (If I put the php file back in the root folder it works fine.
DIR structure
The action and href attributes work exactly same. The final url depends on how you've written the url in these parameters and what is your current url.
Let's say the current url is http://localhost/my-project/html/my-form.html
If your url in action looks like php/contactform.php this is called path relative url. To get final url the browser will append relative url after last / in current url resulting in http://localhost/my-project/html/php/contactform.php.
You can use .. in the relative url if you want to reference directory higher in the structure. Url ../php/contactform.php will result in http://localhost/my-project/php/contactform.php
If your url in action looks like this /php/contactform.php it is root relative url. The main difference is the starting /. In this case the browser will put your url right after the domain name resulting in http://localhost/php/conactform.php.
Based on your screenshot both /php/contactform.php and php/contactform.php should work fine because the url of the html file should be http://your-domain.example/DJ_contactme.html so both urls should result in http://your-domain.example/php/contactform.php
If you are using mod_userdir your url probably looks like http://your-domain.example/~dannyjeb/DJ_contactme.html in that case the php/contactform.php should work as it would result in http://your-domain.example/~dannyjeb/php/contactform.php
From DJ_Contactme.html you target css file like this:
<link rel="stylesheet" href="./css/contactus.css"/>
I'm developing a website for my conclusion work at school. I'm using XAMPP v3.2.1. to localhost the site.
My site's folder's are configured just like this in htdocs folder:
ibnm
css
js
img
...
site
about
midia
...
includes.php
index.php (HOME PAGE)
My problem starts here: on index.php I'm including includes.php, that's a simple file with define() functions to the folders of my site so I can print the constant on the HTML tags of the site as URL (just like below)
//includes.php
<?php
define("css", "localhost/ibnm/css");
?>
//index.php
<?php
include_once("includes.php");
?>
<link href="<?= css; ?>/bootstrap.css" rel="stylesheet">
But when I do this, the CSS don't function. When I see an <a> tag with the previously defined URL on page it looks like
localhost/ibnm/site/localhost/ibnm/css
instead of
localhost/ibnm/css
It's confunsing 'cause if the <a> tag doesn't have any value (href="") it output localhost/ibnm/site/.
What can be wrong? XAMPP or coding?
Any url is not starts with http then browser will assume that its relative path so it will append to your current path, thats why your getting localhost/ibnm/site/localhost/ibnm/css.
And one small correction in your code, its not good idea to hard code server name in the code, better to get server name dynamically. So that you no need to change while deploying your site in real server.
//includes.php
<?php
define("css", $host='http://'.$_SERVER['SERVER_NAME'].'/ibnm/css');
?>
This is because the browser think localhost is a folder, and then do this ontop of the current path, to fix is just add http:// before the localhost
define("css", "http://localhost/ibnm/css");
Instead of using absolute path you can just add one slash before CSS path -
<link href="/<?= css; ?>/bootstrap.css" rel="stylesheet">
Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.