I am not even A PHP newbie. But I need to call BS on a outside consultant who has come to my company and told me something was impossible.
Is it possible in PHP to simply create a webpage that displays the URL requested by the user.
In other words: rather than echoing/Printing "Hello World"
I would like the page to print the URL that the user requested.
This can be accomplished with:
echo $_SERVER['REQUEST_URI']--
for a full list:
print_r($_SERVER)
Another useful one is $_SERVER['HTTP_HOST']
If you're going to do a redirect, you have more footwork to do, but a straightforward solution would be:
header("Location: {$site}?original_request={$_SERVER['REQUEST_URI']}"); - and then handle the rest in the page you redirect to.
I think what you are looking for is the full url. This can be done with.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $url;
Just incase the port is included.
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$url = "http://".$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI'];
Here ya go:
echo $_SERVER['PHP_SELF'];
In a sense, both you and the consultant are correct. In most cases, yes you can get the exact URL. In many cases, the URL will be approximate. In some cases, you might not get anywhere close to the requested URL.
Here are some barriers to building the exact URL:
The #fragment isn't sent
You don't know if :port was present (so you can't know whether to add it or not)
You don't know what outside PHP rerouting was done (eg in .htaccess)
You could get around these by having a hidden input variable that is filled by Javascript with the full URL, which is then posted. But then the argument is "well what if Javascript isn't turned on."
So, I'd say "yes you can" is the practical/pragmatic answer, while "no you can't" is the academic answer.
Related
I was just looking for a little help on the following, unfortunately I haven't been able to find a solution, perhaps due to not knowing what to call it exactly. I can only find solutions for doing the reverse of what I require.
At present I have a user page set up, which can be accessed via a URL as follows:
[DOMAIN_NAME]/client/?client=3
I would like to be able for users to type:
[DOMAIN_NAME]/apples
and be directed to the the prior URL, assuming apples is a valid user name.
I am using PHP and I know I could have just set it up for users to have their own folders etc. but was wondering if there is a URL redirect solution for this. Preferably one that would keep the url as the user has typed it, but show the real information.
Thanks,
Cillian
$fullurl = explode('/',$_SERVER['SCRIPT_NAME']);
$username = $fullurl[count($fullurl)-1];
echo $username;
then check $username e.i it is valid user or not if valid user then redirect to your desire page.
header('Location: http://www.domain.com/userfolder/yourpage.php?userid=$userid');
You Should use .htaccess redirect Method
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
I need to link to a specific page in my Facebook app. The app is not in a page tab, and cannot be in one due to the project constrictions.
This is the url format:
https://apps.facebook.com/myappname
I would need to pass a parameter at the end (like /next.html or ?page=next) so that I can link to the specific page directly from outside the app (from an email).
How would I set this up? My project uses PHP and jQuery. I would love to be able to do this strictly in Javascript if possible.
I have found tons of info on how to deep link a page tab or a mobile app, but not to a regular application. I have found messages stating it's possible, but nothing about how to actually do it anywhere online or on Facebook.
Thanks for your help.
EDIT:
Okay, I got it working in PHP. For anyone else with this issue, this is what I did.
Add a "?" at the very end of the 'Site URL' in your FB app, then create a redirect file similar to this as your app landing page (just use absolute paths instead of relative ones like I did below):
<?php
$query = $_SERVER['QUERY_STRING'];
$params = explode("/", $query);
if (in_array("gallery", $params)) {
header("Location: /gallery.html");
exit;
}
else {
header("Location: /index.html");
exit;
}
?>
This answer is what helped me figure this out:
$_GET on facebook iframe app
I may be missing something here, but why don't you just link to http://apps.facebook.com/yourapp/something.php - this should automatically load your canvas URL, with something.php appended to the path
Obviously this won't work if your canvas URL points to a specific file and not a directory, but plenty of apps do this with success
When you are using the ? all you are doing is issuing a $_GET request, so all of the info you require will exist in the $_GET array.
Rather than query the $_SERVER array, query the $_GET array.
So if you had:
http://myurl.com?info=foobar
You can simply access that info using:
$info = $_GET['info'];
It is good practice to check for the existence first though:
if (isset($_GET['info']))
{
$info =$_GET['info'];
}
else
{
$info="default";
}
Incidently if you use the & character you can have multiple parameters:
http://myurl.com?info=foo&moreinfo=bar
You get a special parameter called app_data that you can use however you want. I've used it in the past to encode a full querystring of my internal app. for example, &app_data=My/Custom/Page
More found in this SO question: Retrieve Parameter From Page Tab URL
I need to code something like that to print value of javascript in php
<?php
$url = ( Value of <script>document.write(top.location.href);</script> );
echo ''.$url.'';
?>
Update:
I need this to break frames, block proxy websites and bots so $_SERVER not useful
Please can anyone help me ?
Thank you
No, that's not possible because PHP (server-side) is run before javascript (client side) code. You can do the opposite though. You will have to resort to one of these:
AJAX
Hidden Field
Query String
Though, if you want to read the current page URL, you could use:
$_SERVER['REQUEST_URI']
To do what you're doing in PHP you could do $url = $_SERVER['PHP_SELF'];
you might not be looking at this the right way. you can see the current path in the url by looking at this variable
echo $_SERVER['REQUEST_URI'];
Read More
Update:
Since you want to prevent your site for being under an iframe, you can google frame busting script, it is done entirely in javascript.
I'm using a form to submit some post information to a PHP script. After the script finishes, I want it to redirect right back to the page the user came from. Right now I'm just using header() with a static URL. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.
Use HTTP_REFERER:
header('Location: ' . $_SERVER['HTTP_REFERER']);
access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.
the way i would do it is use a session variable to store the current page URL everytime it is accessed.
$_SESSION['last_url'] = <get current url>
replace your static url in the header with $_SESSION['last_url']. Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!)
Use REQUEST_URI But watch out for that presiding slash/
I have a php page that takes in a bunch of url parameters and sends out an email. I am trying to have this page call another web page using the same url parameters, after the mail is sent. If I do the following, will my email be sent reliably? Is a redirect what I really want to do?
Update: Thanks for the tips. As you can see by my use of the +, I don't know any php. After reading all the answers so far I have come up with this:
Random code to send email...
file_get_contents('http://www.othersite.com/' . $_SERVER["REQUEST_URI"]. "?". $_SERVER["QUERY_STRING"]);
I believe this should initiate a GET on the other site with all the current parameters, which is exactly what I want. This way I don't have to deal with redirects. Any problems to this solution?
Update 2: Since my url was https, file_get_contents caused me some problems. There are ways to get around this but I just used header for a redirect and all worked well. Thanks everyone!
The question raised in the other answers whether your basic approach is really what you want is valid - check that first. Anyway, if it really is what you want to do (Is your target URL really identical to the one you're on?) you can indeed use
header('Location: http://www.othersite.com/' . $_SERVER["REQUEST_URI"]);
Just note the use of . to concatenate the string instead of +, you can't do that in PHP.
To do it really properly, you could use http_build_url to build a full valid URL from the current GET array. Code from the manual, modified a bit:
<?php
echo http_build_url("http://user#www.example.com/pub/index.php",
$_GET,
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY
);
?>
The header location call will be only called after the mail code so it won't affect your email.
Don't forget to call exit() after your header location call.
Also the string concat operator is not + it's . (dot).
if its the same application, why dont you call the same functions ?
if you want you could do file_get_contents .. instead of a redirect for the same effect.
If you just want to hit that page why not use file_get_contents
$data = file_get_contents('http://www.othersite.com/' . $_SERVER["REQUEST_URI"]);
echo $data;
The benefit with this is you don't have to physically go to the other site if you don't want to, equally if you control the script on the other site you could return a true or false in the HTML which could be checked upon return.
For full compliance (sometimes Chrome will not work with just a Location: header)
header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance
header( "Location: http://www.othersite.com/' . $_SERVER["REQUEST_URI"] );
Another option is to echo the HTML tag:
<meta http-equiv="Refresh" content="1;url=http://www.othersite.com/<?php echo $_SERVER['REQUEST_URI']; ?>">
This allows you to set a delay time for redirecting (usually 1s), which is good in some situations so that the user doesn't become confused by a flash of content. You can put a 'Stand by while we redirect you' message or similar.