I am making a site and I need to get the "root" directory of the site. Like if the filestructure was similar to
CloudShop
\-internal
\-js
...
\-inc
inc.all.php
index.php
\-login
login.php
loginError.php
page.php
In inc.all.php I am getting all the pages in the database so I can display them in the navigation bar and I am setting the links like <a href="page.php?id=XXX>PAGE_TITLE</a> However, when I click on one of the page links from login.php, it takes me to /CloudShop/login/page.php?id=XXX. I want it to take me to /CloudShop/page.php?id=XXX.
You can use path-absolute URIs. Suppose you had at http://www.example.com/shop/login/login.php:
page
The resolved URI would be http://www.example.com/shop/page.php?id=1.
Suggestion: Try the PHP Magic constants:
__DIR__
Related
So, I have a question regarding paths in links. Here's the problem: In my assignment all my links are broken (not all but half of them don't work).
html_start.php includes nav.php and start_html is included in /admin/index.php and so on.
public_html
html_start
nav
styles.css
admin
index.php
user
index.php
index.php
First I've tried using absolute path eg. a href = "/user/index.php" but the assistent warned me that it leads to homepage of my university not my index.php.
If I use relative links, half of them work do to the includes.
Sadly all of these work in my environment but not when it's uploaded to my school so I don't know how it will behave until I upload the assignment and I have only one attempt to correct it.
Now, i use include in almost every page - eg. include( __DIR__.'/app.cfg.php') and that apparently works.
So my question is can I do the same with links? I've read somewhere that it doesn't do the same thing.
Looks like you're working in "root" (localhost?), where absolute links are fine, but when the site gets set up at your school's server, then absolute links point to the school's domain instead of the subdirectory where your project is.
What you need is using <base> tag in combination with relative links. Note that it may mess up your css/js/images, so you'd need to update all paths to be relative (not starting with /). Here's an example:
<html>
<head>
<base href="http://example.com/"/>
[...]
link
The link above would point to http://example.com/dir/page.html. Basically, setting an URL in base tag sets what is the "root" of your website, and all relative links will respect it. You can store the value of base in your site's config and echo it in the template, so when your site is set up on another server you'd update the config file and all links/images/css/etc would work.
I have searched all over the web trying to figure this out and am now trying to get a direct answer from some experienced users. I hope I can explain myself completely.
I know HTML and CSS and some PHP and Javascript, but no mean an expert. This is my questions:
When creating a website by hand (no Drupal, or Wordpress or predesigned templates), The first thing I do is create an index.php file that shows my HTML page layout. The second thing I do is create my links.inc.php file that will show all the links to my pages, ex: Home, About Us, Contact Us. Now on the index.php page I create php include files for the header, footer, and link pages. (these would read header.inc.php, footer.inc.php, links.inc.php) Now here is where I am trying to figure if there is an easier way to do the next step.
My normal steps would next to be to create a home.inc.php, aboutus.inc.php, contactus.inc.php files which will have all the "content" I want shown for each page.
I would then create a duplicate of the index.php and create aboutus.php where I would use the php include function to add the aboutus.inc.php into the "main content" area I would want this information displayed at. Then I would create anther duplicate of the index.php and name it contactus.php and "include" the contactus.inc.php file.
Is there any way to use the index.php file and have all the inc.php files on that page? For instance,
<div id="main">
<?php
include ("home.inc.php");
include ("aboutus.inc.php");
include ("contactus.inc.php")
?>
</div>
Obviously this does not work they way I have it laid out above, it shows all the pages at the same time instead of only showing the one page that is clicked on from the menu. Any suggestions? Is there a different way or am I doing it correctly with creating multiple pages?
Thank you for any help and I hope I was clear, if not I can try to explain a different way.
My suggestion is to include files conditionally, based on a variable that defines the current page.
For example, given the following navigation:
Home
About Us
Contact Us
Configure your index.php file to include external files, something like this:
// determine the requested page, default to the home page
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// check if the requested include file exists
$include_file = is_file($page.'.inc.php') ? $page.'.inc.php' : false;
// if the requested include file exists, include it
if ($include_file) {
include $include_file;
}
Feel free to adjust the logic. For example, if a $page value is not recognized as a valid page on your site, you may want to show a 404 page, default to the "home" page, etc.
Edit
If your include files are in a different directory, you'll need to provide the correct path:
// define the path to includes
$path = 'inc/';
// check if the requested include file exists
$include_file = is_file($path.$page.'.inc.php') ? $path.$page.'.inc.php' : false;
You could send a variable to PHP index.php?action=home; then, inside index make some verifications
if($action=="home") {include index.inc.php; }
else if ($action=="contact") {include contact.inc.php }
and so on.
I have three pages on my localhost 1 is my index page,2 is my universal header page which is under includes folder of and 3 is my html file which is under html folder.The header file is included in both the index file and html file like that...
for index.php-include("includes/header.php");
for html.php-include("../includes/header.php");
and my header has the link of index.php page that is (./index.php)
Now my questions is that when i open my index page and click on link of index page from my header it takes me to same index.php page but when in open html.php page and then click index.php page link from header it does not go to index.php page but it goes to this page-
(localhost/educational%20website/html/index.php) how to solve that.
And i also want to know that write now i am on localhost but when i make my site live is there any need to change the paths because i am making around 150 pages with your technique plaese so please answer me that kind of technique that is used for both localhost and on live
Your are including paths relatively, use a (absolute) base path in your index.php to fix this:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
One way is to define a variable or a constant for the site's url in the header.php file. Then in all your other pages, you could just use this variable/constant when you need to mention the other urls.
Eg(put this as first line in your header.php file):
define('SITE_URL', 'http://localhost/educationalwebsite');
Here, we have defined a constant named SITE_URL. Then in other pages, you are already including this header file. Isn't it? So, this constant will be available in your index.php, html.php and other pages.
And suppose for a link in your html.php file(to point to the index.php), you could use it like this:
Home
If you want to include link to the html.php file residing inside html folder, it would be like:
HTML
By using this way, if you are uploading the whole site to a live server, you only need to change one line, ie. the first line in header.php, where we have defined the SITE_URL constant. Just change it's value to the new URL of the home directory of your website.
Hope this helps
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.
This is the hierarchy of my folder and files:
/website
/admin
/about
editAbout.php
adminHeader.php
adminDashboard.php
adminLogout.php
adminHeader.php is the header of my pages, so it is being included in all pages. It also contains the Logout link that has this code:
Logout
In adminDashboard.php page it works, but in editAbout.php page, it didn't work because the link is not right, it becomes
http://www.domainname.com/admin/about/adminLogout.php
I tried to change the link to
<a href="http://www.domainname.com/admin/adminLogout.php">
and also by using the $_SERVER['DOCUMENT_ROOT'] but didn't work also.
Does anyone know how to reset the link to the right one?
you can use ../ to come back to the parent folder
it becomes enter code hereLogout
Try this:
$_SERVER['SERVER_NAME'] . "/admin/adminLogout.php";
The SERVER_NAME value refers to the domain.
You do not need to add the server name, as the browser will put that in for you. So just make all of your paths absolute (from the web browser's point of view):
/admin/adminLogout.php
Example:
<a href="/admin/adminLogout.php">