Absolute URL Path for PHP Website - php

I am working on a website in a CMS site. I have provided a link in my footer file e.g blogs.php. This page is at the root like "http://www.example.com/new_cms/blogs.php"
When I moves to other files link like "http://www.example.com/new_cms/forums" the footer link is changed to "http://www.example.com/new_cms/forums/blogs.php" but the blogs.php page resides at path "http://www.example.com/new_cms/" . I tried different $_SERVER[] variables but got no luck to get the above path "http://www.example.com/new_cms/" means the server name with the directory where the project is currently running from.
Any one have idea how to get it done will be a great help.
Thanks

This is probably because your link has a relative url in it, like so :
Blogs
So it looks for the blogs.php file in the current directory.
If you want that link to point to the blogs.php file that is in the new_cms folder, you have to use a link like this :
Blogs

You can use absolute URL or try to rewrite your url with a condition.

You can obtain the server name from $_SERVER['SERVER_NAME'] that is the canonical server name specified in the server/virtual host configuration. Besides that $_SERVER['HTTP_HOST'] gives you the host specified in the HTTP request header field Host. In general, these two values are identical. But since both can be manipulated by user input (see Chris Shiflett’s SERVER_NAME Versus HTTP_HOST) you need to be careful when using these.
As for the path, you will probably need to specify the base URL path on our own. Because if you think of URL rewriting techniques, the physical path (file system path) does not need to be the same as the logical path (URL path). So you can not derive the logical base path from the requested URL path, the script file path and the document root.

Related

Defining root_path to access externally

I'm building a local web application for my company, and I`m trying to run it in an xampp webserver. My problem is that I want to setup my root folder one time, and reference it in all my files.
For example, my folder structure is as follows:
root/index.php
root/include/includefiles.php
root/reles/ajustes/ajustes.php
root/classes/html/menu.php
root/classes/html/rodape.php
root/img/head.png
All of my files have to include the files menu.php and rodape.php
Using relative paths I would do "include ../../classes/html/menu.php" and "../classes/html/menu.php"
Until there it's ok, but in my menu.php file I have a link to other files, and I cannot utilize relative paths to link to it, because at index.php the link would be "/img/head.png" and at ajustes.php would be "../../img/head.png"
My solution is to define a root path, and I would link all my relative paths to ROOT_PATH."/img/head.png".
I found some solutions for this which worked. My problem appears when I try to access my application externally, from another computer using my host IP address, I can access my website, but the link appears as "c:/xampp/htdocs...", and I don't want that, I want the links appearing as "http://host-ip/img/head.png".
A good practice in defining the include paths is to add the __DIR__ magic constant before the include path. That way the path is always defined relative to the directory of the current file instead of the working directory.
You should have a different root path for public urls and internal server paths. So I'd recommend using the __DIR__ for includes and other internal server paths and another constant to be used in html and other public paths.
Edit: To clarify: the internal server path is the actual path on the server (www_root/foo/bar) and the public path is the one the server software serves through http (http://example.com/foo/bar)

absolute url paths for localhost to file sytem

I'm trying to use an absolute path to set the href of an anchor. The path however doesn't work, but if I change to a relative path everything works fine.
Works:
include_once('../../Utils/utils/html/banner.html');
Doesn't Work:
include_once('http://localhost/apps/MyVyn/Utils/utils/html/banner.html');
include_once('/apps/MyVyn/Utils/utils/html/banner.html');
What exactly am I doing wrong?
If you want more of an "absolute" path, you can prepend your include path with $_SERVER['DOCUMENT_ROOT']. Just a thought.
include_once($_SERVER['DOCUMENT_ROOT'] . '/apps/MyVyn/Utils/utils/html/banner.html');
Let's take a look at both examples:
include_once('http://localhost/apps/MyVyn/Utils/utils/html/banner.html');
This won't work because the apps folder isn't in the document root. PHP (with appropriate privileges from apache/or whatever) will be able to access anything on the file system. A direct link to 127.0.0.1, localhost, or a website name will only be able to access files on the document root.
include_once('/apps/MyVyn/Utils/utils/html/banner.html');
This won't work either because you're requesting that PHP look for a folder /apps on the top-level folder of your drive. For example, /root/*, but instead, you're saying, /apps/* which does not exist.
Think of /apps/ in terms of Windows such as C:\apps, which does not exist. If you want your PHP to look at a folder in the same directory, you need to remove the leading slash, such as apps/*.

phpgrid in Laravel 4.1 not working with domain masking

I'm using Laravel 4.1. I put phpgrid into the vendors directory. Here are my paths using example domains:
Actual file path to website root: /home/.../htdocs/dashboard/public
Actual file path to phpgrid: /home/.../htdocs/dashboard/vendor/phpgrid
The (example) url to the site is: http://www.site1.com/dashboard/
The SERVER_ROOT is set to: http://www.site1.com/dashboard/vendor/phpgrid
phpgrid works when I use that domain. The problem is that I want to use a shorter domain with masking. So the url I want to use is something like: http://dashboard.myotherdomain.com/, and the virtual host has it pointing to the actual file path to the website root directory above. phpgrid builds the table, but then the AJAX fires and can't get to the vendor directory because it is now below the site root of the masked domain. I thought it would use the absolute path, but it doesn't seem to. Any ideas?
It turns out that the issue was with the AJAX calls. AJAX will not let you call a different domain for security reasons, so setting the SERVER_ROOT to a different domain than my masked domain was failing (silently btw, blank error message in phpgrid). So I was forced to move the phpgrid files and folders into the webroot and change the SERVER_ROOT to a relative path.
A simple fix although I would have preferred to keep the files in the vendor directory.

PHP include with varying URI structure

I am using a .htaccess file to rewrite all URI addresses to take the user to index.php where some code determines which file to include based on the URL.
If the URL however is in a subfolder from that of the header the include function is not fetching the file.
eg.
index.php includes a file located at examplefolder/content/page.php. That file then includes a header on the content page but include('../header.php'); is not working. The URL is example.com/news/latest.
How can I get this to work?
You can use the chdir() function to change the current working directory on the webserver to get your relative paths to work correctly.
To see what the current working directory is, just echo getcwd(); Once you do that, you may realize that you don't need your relative paths and you can just structure the include based off of whatever your current working directory is (if you're rewriting to index.php, then all of your pages will start from the same base path, namely the web root).
If you rewrite all URLs to /index.php, all requests will be working from the / directory (i.e. document root).
So if you are including examplefolder/content/page.php and from there you want to include the file examplefolder/header.php, then that is exactly what you would write, because including a file does not change the working directory in the current thread of execution.
E.g.
include('examplefolder/header.php');

Finding the file path in drupal

Iam a php developer, and iam new to drupal.I have installed a drupal site.
For normal php sites. we can find the file name from the browser path, for modification
for eg:
browser url : www.mysite.com/test/upload.php
File path :test/upload.php.
for drupal:http://localhost/mydreamhouse/article/557
what is file path?
for drupal :http://localhost/mydreamhouse/newslist
what is file path?
Is there any common way to find the file path in drupal?or can any one describe the flow of pages?
A Drupal URL does not have to correspond to a file, most of them are dynamically created from information in the database. See Understanding Drupal paths for more information.
The file path is always index.php (except for a few exceptions like install.php and cron.php). You can disable the clean urls setting to better understand the path you're looking at. With clean urls enabled, there is some rewriting going on to create nice looking url's.
In your case, http://localhost/mydreamhouse/article/557 is in fact http://localhost/mydreamhouse/index.php?q=article/557. In other words, the index.php script is called, which will in its turn interpret the $_GET['q'] variable to serve the proper page.
getcwd() . base_path() . $file->filepath
getcwd() - server path to webroot
base_path() - path to drupal app (if app in root it will be /)
$file->filepath - any file path (for example sites/all/default/files/...)
As mentioned in other answers, there are no files or flow of pages. index.php takes the values of $_GET['q'] and calls the appropriate functions to dynamically generate pages.
You can use $_GET['q'] to access those "file path" portions of the URL, but the more Drupal way is to use arg(), for example, with a path of http://localhost/node/5, you could access node with a call to arg(0) and the 5 with a call to arg(1). This is the Drupaly way to access those parts of the URL.
Drupal also provides utility functions for getting at the base path. The L function l() formats links and takes care of base path for you as well (you just write relative links, it appends base path as necessary); base_path() will return the global of the same name; drupal_get_path() takes some extra Drupal-specific arguments and will generate paths to things like modules and themes with the base path taken care of as well.
Were you looking for more information? Refine your question based on these answers, and check out Drupal's API documentation: http://api.drupal.org/
// path to Drupal dir - don't use $_SERVER['DOCUMENT_ROOT'] - drush commands will fail
getcwd()
// path to config folder - e.g. /sites/default/settings.php
getcwd() . conf_path(); // see conf_path() for details
// path to config file in module folder
getcwd() . drupal_get_path('module','configure') . '/config.yaml'

Categories