$_SERVER['HTTP_HOST'] appends dot to the end of URL - php

I am calling a function that reads $_SERVER['HTTP_HOST'] and render an anchor element with href that is read from $_SERVER['HTTP_HOST'].
On my mobile theme on Android devices this function appends a dot at the end of the url, so it looks like www.example.com. which makes some other functions work improperly.
Upon debugging I realized that it is precisely $_SERVER['HTTP_HOST'] that has this wrong value.
Anyone has this problem or any idea how to fix it?

i dont think its php issue, but this code can resolve your issue.
trim($_SERVER['HTTP_HOST'], '.')

In the Domain Name System, and most notably, in DNS zone files, a fully qualified domain name is specified with a trailing dot. For example,
somehost.example.com. specifies an absolute domain name that ends with an empty top level domain label.
So the PHP is actually returning the correct value. As to how to combat this, use sudhakar's suggestion.

Related

PHP: How to get content of another URL (page) on my site without changing address in bar

I am using ModRewrite as below to convert urls on my site to be SEO friendly:
RewriteRule user/(.*)/$ seo-url-user-by-name.php?username=$1
Now I am writing code for seo-url-user-by-name.php and am looking for a way in PHP to redirect to:
user.php?uid=<uid>
so that seo-url-user-by-name.php will essentially return the contents of user.php?uid=<uid> BUT without changing the address in address bar to user.php?uid=<uid>
How do I do that?
Simply include user.php in seo-url-user-by-name.php. To get the querystring right you have to overwrite the value in $_GET.
$_GET['uid'] = 'whatever you want';
include 'user.php';
You're going about it backwards. The only URLs your code should be outputting are the 'friendly' ones. Those are the urls that will appear in the produced HTML and what will show up in the user's address bar.
e.g.
Bad URL (URL #1)
This URL is fine (URL #2)
You should never output anything but URL #2's. It'll be your server's responsibility to convert that clean (and in real terms, non-existent) URL to whatever really is on the server. PHP itself should never care nor see the /user/foo URL. PHP will be invoked as /user.php?id=foo as usual, and go about its business as usual.
The remote user would never see that rewriting occurring, they'll just see a request go out for /user/foo.

Php include and redirects

I've been wandering for a while trying to get out of this problem.
I have a php software in which I want to know where the script is being executed.
e.g.
I have /foo/bar/home.php . In this case I would like to know that /foo/bar is my root. But if I have an example admin page /foo/bar/admin/index.php I would like to have /foo/bar in this case too.
Or
/foo/bar/foo/index.php -> /foo/bar/foo
/foo/bar/foo/randomname/home.php -> /foo/bar/foo
How can I accomplish that ?
Thanks for help
Because /foo/bar does not really sound like a filesystem path to me, I assume that this is the first part of the URL path.
How does the application know that this prefix is applied to everything? It must be installed somewhere, and if no rewriting is applied, the filesystem path layout and url path layout match on some level. This might be used to actually generate path information by subtracting some strings, but I doubt it's usefulness.
I'd opt for defining a constant "INSTALL_PATH" and/or "INSTALL_URL" in a file that is included everywhere, which knows its relative location to the base url or file path, and does a simple string operation:
define('INSTALL_PATH', basename(__DIR__); // go one level up
You can access the properties of the $_SERVER superglobal variable.
echo $_SERVER['DOCUMENT_ROOT'];
The following, if located in "/foo/bar/foo/index.php", would echo /foo/bar/foo/.

PHP redirect to another website

I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:
<?php
$url = $_GET['url'];
header("Location: $url");
?>
At the moment to redirect to another URL I have to type this into the address bar:
http://mysite.com/goto/?url=http://google.com
I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:
http://mysite.com/goto/http://google.com
Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com
RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]
Depending on your server configuration you may need to include a / in your rewrite path (i.e., ^/goto/(.+)$).
Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.
If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.
You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.
My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.
EDIT
If you're determined then parse_url() is what you want. :)
#ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'], which is how that part of the URL would get passed to the CGI script.
(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)

Return current web path in PHP

Currently developing a PHP framework and have ran into my first problem. I need to be able to drop the framework into any folder on a server, no matter how many folders deep, and need to find that directory to use as a base URL.
For example, it currently works if I put the framework in the root of the server (http://cms.dev/), but if I were to put it in http://cms.dev/folder/ it does not work.
There are four existing answers, but they all seem to deal with file paths, and you're asking about a base URL for web requests.
Given any web request, you get a bunch of keys in $_SERVER that may be helpful. For example, in your mock example, you might have the following:
http://cms.dev/folder/ — $_SERVER['REQUEST_URI'] == /folder/
http://cms.dev/folder/index.php — $_SERVER['REQUEST_URI'] == /folder/index.php
http://cms.dev/folder/index.php/some/pathinfo — $_SERVER['REQUEST_URI'] == /folder/index.php/some/pathinfo
http://cms.dev/folder/some/modrewrite — $_SERVER['REQUEST_URI'] == /folder/some/modrewrite
Thinking critically, how would you pull out the base URL for any given subrequest? In certain cases you can look at $_SERVER['REQUEST_URI'] and strip off trailing elements if you know how deep in your hierarchy the request is. (For example, if your script is two folders deep, strip off the last two path elements.) When PATH_INFO or mod_rewrite are in use, things become less clear: as longer and longer URLs are provided, there is no clear indication where the paths end and the dynamic URL begins.
This is why WordPress, MediaWiki, phpBB, phpMyAdmin, and every application I've ever written has the user manually specify a base URL as part of the application configuration.
__FILE__ is a magic constant that returns the entire path of the current script. Combine with dirname and add ".." appropriately. It's more reliable than getcwd, since it cannot change during execution.
You can then strip off the web root, to get the relative path to your script (should map to URL). There are many $_SERVER variables that have this information. I suggest using the file system to determine:
If your script is publicly accessible?
At which depth / URL prefix?
Then combine with your base URL. If your script's path ==
/home/public/foo_1/script.php
... and your $_SERVER['DOCUMENT_ROOT'] ==
/home/public
Then you can rewrite your URL as /foo_1/script.php. You don't need the fully qualified URL, unless you want it. This technique works best if you execute it from a central location, like an autoloader.
In order to make urls work check the base tag:
<base href="http://cms.dev/folder/" />
If the PHP file paths are the issue go with Pestilance's advice:
dirname(__FILE__) // returns the directory of current file
Theres a bunch of useful stuff available for things like this in the $_SERVER array. Do a var_dump($_SERVER); to see which element(s) of the array you need.
dirname(__FILE__);
basename(__FILE__);
print_r($_SERVER);
pathinfo('www/htdocs/index.html');
realpath('../../dir1/');
parse_url('http://username:password#hostname/path?arg=value#anchor');
What you are looking for is the WEBROOT of your application based on your description. The checked answer is, by far, the closest answer.
The easiest way to identify the webroot is to have it be user defined, as was mentioned by Annika and noted in a comment.
However, there is a bit of information that was overlooked:
If you are trying to identify the location of the webroot, which coincidentally is also the top level of your framework, then you could use something like this:
$web_only_path = dirname($_SERVER['PHP_SELF']);
This will only work if your rewrite conditions are implemented correctly.
If they are in an htaccess file, no sweat.
However, if they are in an apache conf file. Then they must be contained within a container for the SERVER variable to store and return the proper information after working through the redirects when dealing with the SEO friendly URLs.
See:SCRIPT_NAME and PHP_SELF with mod_rewrite in .conf
To get the current path of the file you must use:
$path=getcwd();
This will return you if in windows for example C:\blah\blah\blah with no file name.
Sounds like you are looking for the relative path.
$_SERVER['SCRIPT_FILENAME']
should do the trick. The php.net site has good documentation on what is available to that http://php.net/manual/en/reserved.variables.server.php
Also, if you are ever curious about what else is there, and what the values are
<?php print_r($_SERVER); ?>
will tell you more that you thought you could know.
function GetCurrentWebDir() {
$CurrentPath = substr(
$_SERVER['SCRIPT_URL'], 0,
strlen($_SERVER['SCRIPT_URL']) - strlen(
strrchr($_SERVER['SCRIPT_URL'], "\\")
)
);
$CurrentFileName = basename(__FILE__);
return substr_replace(
$CurrentPath, '', -strlen($CurrentFileName),
strlen($CurrentFileName)
);
}
echo 'current dir:'.GetCurrentWebDir();`
cp:/apps/PHP/testtesttesttesttesttesttesttesttesttesttest.php
fn:testtesttesttesttesttesttesttesttesttesttest.php
len:48
current dir:/apps/PHP/
Use dirname(__PATH__) to fetch the parent directory path.

Fix links addresses on websites HTML code

i have been working on one tool lately. It grabs all the link addresses from the website.
My problem is that links in html code sometimes is different:
/index.php
index.php
http://www.website.com/index.php
I need to make all links same:
/index.php -> http://www.website.com/index.php
index.php -> http://www.website.com/index.php
http://www.website.com/index.php -> http://www.website.com/index.php
Thanks for help.
Using preg_replace to fix relative urls
Requires:
$domain = the subject sites domain
$path = the document or string you are looking for relative links with in.
Returns:
$url = the doument or string with the links within it converted to proper urls with the domain given.
Code:
$url = preg_replace('<a\shref="([\/\?\w\.=\&]+)"([\s]rel="(\w+)")*>/', '<a href="http://{$site_domain}$1" rel="$3">' $path)
good luck, let me know how it goes.
Welcome to GoogleOverflow.com.
Here is the complete tutorial for parsing links in HTML using PHP and regex: http://www.the-art-of-web.com/php/parse-links/
Here's a function which will return the absolute URL given the base (current) URL and a relative one.
You need to check for the existence of a base tag. If you find it, it specify the base URL (otherwise, the base URL is the same path the browser points to, up to the last /).

Categories