Navigating to another page in php: - php

I use that function to navigate to another page:
define('WEB_ROOT', '/');
function redirect($location)
{
header('Location: ' . WEB_ROOT . $location);
die();
}
And I deliver it the following address:
$lastPageTransfer=$_SERVER["SCRIPT_NAME"]."?threadID=".$threadID."&page=".$pager->MaxPage;
redirect($lastPageTransfer);
It doesnt matter what the get variables are.. what I get is this:
http://politicalforum/Thread/thread.php%20%20%20?threadID=23&page=4
when the navigation completes and I get an error from google saying it cant find the page.
But I do get it to work, if I put the $lastPageTransfer into an attribute of a link
<a href=...:
http://localhost/PoliticalForum/Thread/thread.php%20%20%20%20?page=2&threadID=23
Another thing to ask.. why are the %20 generated adn how can I prevent it?!?
UPDATE:
I get this:
http://localhost/localhost//PoliticalForum/Thread/thread.php?threadID=23&page=5
when I do this:
$lastPageTransfer=$_SERVER['HTTP_HOST']."/".$_SERVER["SCRIPT_NAME"]."?threadID=".$threadID."&page=".$pager->MaxPage;

%20 is an encoded whitespace. You avoid it by not having whitespaces in your links.
Regarding the link I'll take a shot on that you haven't supplied a host name:
http://politicalforum/Thread/thread.php%20%20%20?threadID=23&page=4
^
which could be:
http://www.example.com/politicalforum/Thread/thread.php%20%20%20?threadID=23&page=4
^^^^^^^^^^^^^^^
Were www.example.com should be your host name. In the case were you use it in a link it's included localhost as hostname automagically. The behavior comes from that when using <a href.. you don't need to have full paths.
From the manual:
HTTP/1.1 requires an absolute URI as argument to » Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself

This is called URL encoding/escaping. To fix that, you need to unescape the URL.

Related

php get URL of current file directory

Might be an easy question for you, but I'm breaking my head over this one.
I have a php file that needs to know it's current directory url to be able to link to something relative to itself.
For example, currently I know to get the current directory path instead of the url. When I use this I get the path:
realpath(__DIR__)
result:
/Applications/MAMP/htdocs/mysite/dir1/dir2/dir3
But this would be my desired result:
http://localhost:8888/dir1/dir2/dir3
Note that this is not the location of the current page. The page calls a file from "http://localhost:8888/dir1/dir2/dir3/myfile.php"
And "myfile.php" has the script from above.
-- edit to elaborate more details --
Thanks for your answers. But I get that I need to add more detail.
http://localhost:8888/index.php calls: "http://localhost:8888/dir1/dir2/dir3/myfile.php"
"myfile.php" needs to know it's place in the universe :) "Where am I"
"myfile.php" should know it's url location is "http://localhost:8888/dir1/dir2/dir3/"
Use echo $_SERVER['PHP_SELF'];
For example if the URL is http://localhost/~andy/test.php
The output would be:
/~andy/test.php
That's enough to generate a relative URL.
If you want the directory your current script is running in - without the filename - use:
echo dirname($_SERVER['PHP_SELF']);
In the case above that will give you /~andy (without test.php at the end). See http://php.net/manual/en/function.dirname.php
Please note that echo getcwd(); is not what you want, based on your question. That gives you the location on the filesystem/server (not the URL) that your script is running from. The directory the script is located in on the servers filesystem, and the URL, are 2 completely different things.
There is also a function to parse URL's built in to PHP: http://php.net/manual/en/function.parse-url.php
If your URL is like this: https://localhost.com/this/is/a/url
$_SERVER['DOCUMENT_ROOT'] - gives system path [/var/www/html/this/is/a/url]
$_SERVER['PHP_SELF'] - gives the route of the current file (after the domain name) [/this/is/a/url]
$_SERVER['SERVER_NAME'] - gives the domain name [localhost.com]
$_SERVER['HTTP_REFERER'] - gives the correct HTTP(S) protocol and domain name. [https://localhost.com]
If you would like to get the full url, you can do something like:
echo $_SERVER['HTTP_REFERER'] . $_SERVER['PHP_SELF'];
However, I do believe in this case, that all you need is the relative path.. and in that case you should only need to use $_SERVER['PHP_SELF'];
I've found a solution here:
https://stackoverflow.com/a/1240574/7295693
This is the code I'll now be useing:
function get_current_file_url($Protocol='http://') {
return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__));
}
Based on your question, I believe this will get you what your want:
$_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/"));
Reference:
$_SERVER['HTTP_HOST'] - In your case this would return: http://localhost:8888
$_SERVER['REQUEST_URI'] - In your case this would return: /dir1/dir2/dir3/myfile.php
With the added substr() and strrpos() methods, you can strip the _myfile.php` off of the end to get the desired result:
http://localhost:8888/dir1/dir2/dir3

external link sends me internally

I am have a form that requests a user to submit a website and then on a different page I send a mysql query to retrieve that website an and turn it into a link by doing this in PHP (V=5.6)
$link = '' . $school_website . '';
the problem is that when i try to click this link, instead of sending me to www.google.com for example, it directs me to www.mydomain.com/www.google.com.
I fixed it originally by using substr to see if the first part was "www" and if it was adding "http://" to it and it worked but now i realize that not all websites will start out with that.
I have tried googling this problem but Im not quite sure how to word the problem so I am not getting anything.
My apologies if this is a duplicate. I did not see anything here that fits my problem, so any help would be greatly appreciated. Thanks.
Could always check if it has http/s:// with regex, if it hasn't then add http:// and the link will work as it should. Or make it ugly but simple.
Simplest way is to remove any protocol and prepend // - that would mark the link as absolute and adopt your current protocol. Even if it didn't have http/s:// it would work as it should.
$school_website = '//' . str_ireplace(['https://', 'http://'], '', $school_website);
Example:
https://google.com becomes //google.com
google.com becomes //google.com
www.google.com becomes //www.google.com
In any of the above cases it would become a absolute url.
A better but longer way would be to validate the url with regex.
Until you add http:// or https://in front of url. It will remain the relative
Like if you re on www.mydomain.com and your href attribute value is www.google.com, The attribute value remain the relative and will target to
you url.
You need http:// or https:// at the beginning of the URL of external links - in fact that should be part of your variable "$school_website", and if that one is for example retrieved from a database, that value has to be changed in the database.

php header function not redirecting as needed

This is a simple question which makes it painfully obvious that I need to take a php class...
I have as the first part of a an if / else statement that reads:
if (is_user_logged_in()){
//echo "user is signed in<P>";
header("Location: user-homepage.php");
so if the user is logged in and clicks a link that directs to /register.php, they should instead be redirected to the user-homepage.php.
What happens is they are directed instead directed to /register.php/user-homepage.php
My code is adding /user-homepage.php to the address instead of replacing /register.php with /user-homepage.php
What have I done wrong?
Use an absolute path rather than a relative one:
header("Location: /user-homepage.php");
Try:
header("Location: http://your_domain.com/user-homepage.php");
The PHP manual says to use Absolute URLs.
You have used a relative file path. Try adding a forward slash to make it relative to the domain root.
header("Location: /user-homepage.php");
As given in section 14.30 of RFC 2616, "HTTP 1.1", use an absolute URL in the Location header.

PHP skipping script and click anchor tag

I have a form. After the form is filled out and the user clicks the submit button the user is taken to a thank you page. On the thank you page is a link (anchor tag) for the user to get to her home page. It works fine 19 out of 20 times.
The problem is, sometimes php skips the thank you page and goes directly to the home page. How is this possible? How is php clicking the link? I've gone over the code and it's completely correct. There is no javascript, just html and css.
Like I said, it doesn't do it every time and I guess it's not a bid deal I'd just like to understand what's going on. I'm using a simple header redirect like so
$url = "thanks/";
header("Location: $url");
exit();
What do you guys think is going? Is there any way to stop it?
Thanks
The RFC for the Location header requires a single absolute URI. This is also pointed out in the PHP manual in the notes section:
HTTP/1.1 requires an absolute URI as argument to » Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself
The problem may be to do with the fact that you're passing non standard headers to the browser. Browsers interpret the malformed header string differently and don't always behave as expected. Again as demonstrated in the PHP manual you should create an absolute URI, not an absolute or relative path before passing it to the header() function.
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;

Get current page URL and title in WordPress?

How to get current page URL and title in WordPress?
Here are few super global variables for getting url info:
$_SERVER['PHP_SELF'];
$_SERVER['REQUEST_URI'];
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_NAME'] will be same - /index.php. It is irrespective of the actual URI ($_SERVER['REQUEST_URI']) used to access the site.
As it returns the actual script name, it fails provide additional path information that may be present. So if the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then too the $_SERVER['SCRIPT_NAME'] will be same - /index.php.
$_SERVER['SCRIPT_NAME'] is supported on all platforms
$_SERVER['PHP_SELF']
This is the filename of the currently executing script, relative to the document root. However, unlike $_SERVER['SCRIPT_NAME'], it provides additional path information like $_SERVER['REQUEST_URI'] when the actual php file is present in the path. So when the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then $_SERVER['PHP_SELF'] will be /index.php/big/directory/.
However if all the URI's under http://www.example.com/ is mapped to http://www.example.com/index.php, then, for example, http://www.example.com/abc/def will return /index.php like $_SERVER['SCRIPT_NAME']. Note that $_SERVER['REQUEST_URI'] data is ignored for this request.
$_SERVER['REQUEST_URI']
It will give you entire url including query string vars.
Title of the page:
There is no built-in functionality to get title of the page, however, you can use the HTML Simple DOM to read the contents of <h2.
Example:
$title = str_get_html('<title></title>');
Now, you can use $title however, you like. Visit the their site for more info about it.
If you are using wordpress, and you want to print the post title you can use,
<?php echo get_the_title(); ?>
documentation here: http://codex.wordpress.org/get_the_title
you can get the URL by
'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
or
$_SERVER['SCRIPT_URI']
might work on some platforms.
the title is something that the PHP program gives back to the browser via HTML:
<html><head><title>title is here</title> ...
so it is not something that you "get" unless you get something from the DB and send back to the browser.
Update... do you mean the title of page that leads to your php file? (the user clicks on that page to get to your php file.) in that case, you can use $_SERVER['HTTP_REFERER'] but it is not guaranteed to contain any data (but most of the time it will).

Categories