Directory path calls, strange behaviour - php

I have an ajax request that I'm trying to call a specific file from which is located at:
ROOT/admin/functions/upload/filename.php
And the page making the request from is located at:
ROOT/admin/customers/123
Which is modified through htaccess from
ROOT/admin/customer.php?id=123
I have tried every combination of paths I could think of but I get some strange behaviours for example when I use
../functions/upload/filename.php
It looks for the file in
ROOT/functions/uploads/filename.php
And when I use
functions/upload/filename.php
It looks for the file in
ROOT/admin/customers/functions/uploads/filename.php
So I tried
../admin/functions/upload/filename.php
And it looks in
ROOT/admin/admin/functions/upload/filename.php
I'm pulling my hair out here, has anyone got any ideas as to what this might be?
Any help would be greatly appreciated.
Thanks, James.

Since the browser knows NOTHING about your server-side paths, and it only has the path you see in the address bar, e.g.
http://example.com/ROOT/admin/customers/123
then if your ajax code looks like
$.ajax('functions/foo/bar.php');
Then the ajax call will be requesting
http://example.com/ROOT/admin/customers/123/functions/foo/bar.php
Similarly, adding ../ just strips off levels of the source page's address:
$.ajax('../../functions/foo/bar.php');
results in
http://example.com/ROOT/admin/customers/123/../../functions/foo/bar.php
^-A-^
^-------B------^
http://example.com/ROOT/admin/functions/foo.bar.php
You probably want
$.ajax('/ROOT/functions/foo/bar.php');
With that leading /, the browser ignores ALL of the subdirectory stuff in the url and uses the entire path from the ajax call as the entirety of the path.
http://example.com/ + /ROOT/functions/foo.bar.php

Related

Can a forward slash at the end of a url effect the pages formatting?

I have an .htaccess file that redirects all pages to index.php.
INDEX.PHP
<?php
$url = $_SERVER['REQUEST_URI'];
$url_array=explode("/",$url);
array_shift($url_array);
$url = $url_array[0].'.php';
include($url_array[0].'.php');
?>
This page takes all incoming url's that are formatted as www.sitename.com/profile/01234 and stores each part in a separate row of an array. the first row ($url_array[0]) is the name of the page (profile) and gets put in the include statement.
When I go to www.sitename.com/profile it includes "profile.php" perfectly but as soon as I add a / or /01234 then the formatting of "profile.php" gets all messed up.
I've used print_r($url_array); to check the array and everything looks fine.
Array ( [0] => people [1] => 01234 )
I've spent hours trying to figure out how a / at the end of the url would effect the layout/stylesheet of the included file but without any success. Any help would be greatly appreciated.
IF your browser thinks you are at www.sitename.com/profile/01234 and tries to load a css stylesheet or js file, for example src='style.css', your browser will try to load www.sitename.com/profile/style.css. What you want to do is tell the browser to always look for that file at the root of your domain by making your css/js sources src='/style.css'. This way, it is always relative to your root path.
This is probably due to relative ccs paths in your html, adding the last / leads to none existing css paths to be generated.

PHP include seems to make page interpret itself as a directory

I have a page, index.php, like this (simplified):
<img src="content/banner.png"/>
The image loads fine. But when I include a certain (WordPress) script, like so,
<img src="content/banner.png"/>
<?php
include('template-loader.php');
?>
the image no longer loads! I also notice the URL in the browser becomes index.php/.
How is this possible? The image of course won't load because I'm using relative paths and it's looking for index.php/content/banner.jpg. True, I can use absolute paths to fix this issue, but I'd like to know what could be happening and how the problem might be fixed in PHP.
I'm testing on a local wamp installation and have disabled all mod_rewrites and such, to be sure it's not something external causing the problem.
D'oh. Minutes after posting, I found the culprit, right in the script:
/**
* Loads the correct template based on the visitor's url
* #package WordPress
*/
if ( defined('WP_USE_THEMES') && WP_USE_THEMES )
do_action('template_redirect');
It's doing some kind of a redirect; commenting this out makes everything work as expected.
I still don't really understand how this script can magically redirect to index.php/ as if it were a directory (and have the document actually load) but...
I will delete this question in the future if it doesn't seem to be helping anyone (based on views and votes).

Setting new absolute path of resources within Javascript depending on environment

I have an external javascript file that uses the getScript() function to run another JS file.
I have those all on static.mydomain.com. (I'm new to setting up CDNs)
getScript() doesn't seem to allow cross-domain requests because my HTML is on domain.com. But then I tried using relative paths according to this post: Dynamic URLs in CSS/JS
It works for CSS but does not work for JS (specifically within the getScript() function). What's going on here? What are some ways to mitigate this problem when dealing with CDNs?
The getScript method actually makes an ajax call, hence the reason it's not working. Unless you need access to things like 'was the script successfully found' and the like, it's better to just write up a quick method like...
function addScript(source, domain) {
$("head").append("<script src='"+ (domain ? domain + source : source) +"'></script>");
}
That will just add scripts to the head of the page, and let you add an optional domain to point to in case you want to change it up.

How to detect the path to the application root?

I'm trying to dynamically detect the root directory of my page in order to direct to a specific script.
echo ($_SERVER['DOCUMENT_ROOT']);
It prints /myName/folder/index.php
I'd like to use in a html-file to enter a certain script like this:
log out
This seems to be in bad syntax, the path is not successfully resolved.
What's the proper approach to detect the path to logout.php?
The same question in different words:
How can I reliably achieve the path to the root directory (which contains my index.php) from ANY subdirectory? No matter if the html file is in /lib/subfolder or in /anotherDirectory, I want it to have a link directing to /lib/logout.php
On my machine it's supposed to be http://localhost/myName/folder (which contains index.php and all subdirectories), on someone else's it might be http://localhost/project
How can I detect the path to application root?
After some clarification from the OP it become possible to answer this question.
If you have some configuration file being included in all php scripts, placed in the app's root folder you can use this file to determine your application root:
$approot = substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']));
__FILE__ constant will give you filesystem path to this file. If you subtract DOCUMENT_ROOT from it, the rest will be what you're looking for. So it can be used in your templates:
log out
Probably you are looking for the URL not the Path
log out
and you are not echoing the variable in your example.
Your DOCUMENT_ROOT is local to your machine - so it might end up being c:/www or something, useful for statements like REQUIRE or INCLUDE but not useful for links.
If you've got a page accessible on the web - linking back to a document on C: is going to try and get that drive from the local machine.
So for links, you should just be able to go /lib/logout.php with the initial slash taking you right to the top of your web accessible structure.
Your page, locally - might be in c:/www/myprojects/project1/lib/logout.php but the site itself might be at http://www.mydomain.com/lib/project.php
Frameworks like Symfony offer a sophisticated routing mechanism which allows you to write link urls like this:
log out
It has tons of possibilities, which are described in the tutorial.
Try this,
log out
This jumps to the root directly.
DOCUMENT_ROOT refers to the physical path on the webserver. There is no generic way to detect the http path fragment. Quite often you can however use PHP_SELF or REQUEST_URI
Both depend on how the current script was invoked. If the current request was to the index.php in a /whatever/ directory, then try the raw REQUEST_URI string. Otherwise it's quite commonly:
<?= dirname($_SERVER["SCRIPT_NAME"]) . "/lib/logout.php" ?>
It's often best if you use a configurable constant for such purposes however. There are too many ifs going on here.
I'm trying to figure this out for PHP as well. In asp.net, we have Request.ApplicationPath, which makes this pretty easy.
For anyone out there fluent in PHP who is trying to help, this code does what the OP is asking, but in asp.net:
public string AppUrl
{
get
{
string appUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
if (appUrl.Substring(appUrl.Length - 1) != "/")
{
appUrl += "/";
}
// Workaround for sockets issue when using VS Built-int web server
appUrl = appUrl.Replace("0.0.0.0", "localhost");
return appUrl;
}
}
I couldn't figure out how to do this in PHP, so what I did was create a file called globals.php, which I stuck in the root. It has this line:
$appPath = "http://localhost/MyApplication/";
It is part of the project, but excluded from source control. So various devs just set it to whatever they want and we make sure to never deploy it. This is probably the effort the OP is trying to skip (as I skipped with my asp.net code).
I hope this helps lead to an answer, or provides a work-around for PHPers out there.

get URL in php IN INCLUDE FILE

i have some problem i try to get the uri in php.
I'm using:
$_SERVER['REQUEST_URI']
It works just fine if i do it in the index.php, but, i NEED to get the url in a include file, but, when i do it, it takes the FILE adress, i mean, it shows something like this
adress bar: www.webpage.com/index.php
$_SERVER['REQUEST_URI'] output: webpage/includefile.php
I am explaining myself here? Thanks!
How are you including the file? If it's being included via an HTTP reference then it's actually being served as a page and the functionality you are seeing is correct. If the include path is a local file, you shouldn't be seeing this behaviour
Found this whilst trying to solve the same issue.
My solution that worked is to use $_SERVER['HTTP_REFERER']
This worked well in that it also included the parameters (e.g. ?this=that&foo=bar)
Maybe somewhere in your code (or in another include file) the value is overwritten.

Categories