redirect with maipulated host header in php - php

there is a target site like https://target.com/ and i have hosted another site likehttp://mysites.com/index.php.
when users connect to my site i want to redirect them to the target site with manipulated Host header parameter(for example if in normal the host header is target.com i want the user request the redirected site with Host:google.com host header value).
what is the simple way to do it ??? i have already tried header in php and didnt get answer and i dont have enough knowledge to use cUrl to redirect users with new host header.

Redirect in PHP can be done with header function: http://php.net/manual/ru/function.header.php In your case it'll look like this:
header("Location: http://target.com/");

Related

Automatically get URL of a site displaying my image?

I've been trying to get the URL (including GET parameters) of a site that is displaying my image. This is because I want to extract one parameter of the URL.
A friend told me that she knew someone that could achieve this, but I don't know if he was doing it with an image. Also I don't think I can do it with a link because when going to external sites it will appear a warning page saying that you're being redirected outside, so if I put a link to my page and someone clicks, I will get the referrer URL of redirection warning page. I can't assure if my friend was telling the truth about this, but it's very likely that it was true.
All I could get with the image was the IP and other things of the HTTP header, but the referrer part is empty and I thought that the referrer contained the full URL I'm talking about.
This is what I have tried.
First the img tag in the other site in BBCode:
[img]http://______.com/get_image.php?i=myimage[/img]
And in my site this script in PHP, although any language that does the work would be good for me:
<?php
// Get name of image to be displayed (non-sanitized here for simplicity)
$filename = $_GET["i"];
// Here I want to get the site where image is being viewed
if (!empty($_SERVER['HTTP_REFERER'])) {
$visitor_url = $_SERVER['HTTP_REFERER'];
} else {
$visitor_url = "none";
}
// And write the referrer to a file just to test if it works
$fp = fopen('referer.txt', 'w');
fwrite($fp, $visitor_url);
fclose($fp);
// Eventually display the image
header('Content-Type: image/png');
readfile($filename . '.png');
?>
So my questions are:
Is it possible to get full URL of a site that is displaying my image?
If not, is there any other method to get the full URL?
Thank you in advance.
Note: I don't have any permision in the other site where I'm posting the image, I'm just an user there. Please tell me if I'm missing something or I have to ask this in another way, I'm new to StackOverflow.
Try REMOTE_HOST instead of HTTP_REFERER:
// Here I want to get the site where image is being viewed
if (!empty($_SERVER['REMOTE_HOST'])) {
$visitor_url = $_SERVER['REMOTE_HOST'];
} else {
$visitor_url = "none";
}
The web server where you are serving the image will need to be configured properly. If using Apache, this is with HostNameLookups On.
See http://php.net/manual/en/reserved.variables.server.php
Normally browsers are sending full referer with all URL components including query parameters - $_GET params. If they don't then there is no other way to achieve that URL while passing throught an image content.
Sometimes sending referer may be blocked, for eg. in some batch URL processing using some crawler like program/script or on some proxies.
In PHP receiving referer is done by $_SERVER['HTTP_REFERER'] because it's normally just http header from request and it's the only $_SERVER array key with referer info.
You added the .htaccess tag so I think you're using the Apache web server. If you'd like to prevent the issue entirely, you can disable hotlinking entirely by going one layer lower. Instead of managing in PHP, you can configure the web server to not serve content to domains other than the one you are hosting.
Check out the guide for more details.
I fixed this problem by switching my site (where image is hosted) to HTTPS. The code in my question was doing its job correctly.
It looks that HTTP_REFERER was blank because of it coming from an HTTPS site and my site being HTTP it would always send it blank. I was aware that it could be a problem, but didn't make much sense for me because HTTP_REFERER was also blank when coming from another HTTP site (which I think it's not normal) so I thought the error was in another place.
Usually HTTP_REFERER is sent when it comes from and goes to:
from HTTP to HTTP
from HTTPS to HTTPS
from HTTP to HTTPS
But it's not sent when it comes from and goes to:
from HTTPS to HTTP
And in my case, I don't know why, it wasn't being sent from HTTP to HTTP which was confusing me.

php redirect triggers warning from page that is redirect to

I am trying to redirect my domain name to my corporate sub domain site. I have used php to redirect the page but when I try to go there Firefox gives me the following warning.
"You are about to log in to the site "######.com" with the username "myRedirect", but the website does not require authentication. This may be an attempt to trick you.
Is "#####.com" the site you want to visit?"
When I click yes it sends me to the top level domain and not my sub domain. I have no control over the corporate domain or the sub domain.
I have used both of these methods:
<?php
echo "<meta http-equiv='refresh' content='0;url=http://myRedirect#subdomain.com'>";
?>
<?php
header("http://myRedirect#subdomain.com");
?>
Is there a way to do the redirect as not to trigger the warning event?
The reason for this error is that you put url http://myRedirect#subdomain.com which is user myRedirect without password and subdomain.com as domain but this only work if that page have HTTP Authenication. (popup prompt to enter user and password when call without user and password).
to redirect you should use url subdomain.domain.com and another thing is that in php you need to use Location header.
header('Location: subdomain.domain.com');

Redirect to a different domain in PHP

I am trying to redirect www.xyz.com/my to www.abc.com, how can I do this in PHP?
Strictly speaking you need to send a HTTP location header to the clients browser.
To do this in PHP, as the other answers have mentioned is to use the header() function.
header("Location: http://www.abc.com");
There is a caveat that you should be aware of. The most common problem people encounter when dealing with HTTP headers is that they must be sent to the browser prior to any other data. If you echo any content to the client and then try to send the location header, it wont work.
For reference, there are many other HTTP headers that you should familiarize your self with.
You may do that in PHP by creating a subdirectory called /my on the http-root of the server where www.xyz.com is hosted and then make an index.php file with <?php header("Location: http://www.abc.com"); ?> inside it.
However, this is not the right way to do it. What you should do it create a .htaccess file right in the root folder of www.xyz.com looking like:
redirect /my http://www.abc.com
You can send a header redirect:
header("Location: http://abc.com");
If the default page for a directory is index.php, just put that code under /my/index.php. Any visits to the /my page will redirect.
header("Location: http://www.abc.com");
wrong technology. You probably want to look into apache htaccess, which is generally what most people use to host php.

Is it possible a Search Engine Friendly Redirct with Interface page?

I want to redirect page like: http://www.mysite.com/index.php?id=1 to http://www.mysite.com/the-real-name.htm but I don’t have the-real-name in first url then I should get it from db.
I made an interface page then I redirected page http://www.mysite.com/index.php?id=1 to it and I fetched the-real-name (with id parameter in url) from db then I redirected page to http://www.mysite.com/the-real-name.htm with PHP Header function.
Is this process search engine friendly?
Which page will be indexed with search engine crawler? Interface page or http://www.mysite.com/the-real-name.htm ?
Which is the best solution for indexing http://www.mysite.com/the-real-name.htm ?
Thanks a lot
If you want to tell the search engine that the final URL is "the URL", you need to do a permanent redirect. The HTTP status code is 301.
header('Location: http://www.mysite.com/the-real-name.htm', true, 301);
For the first redirect, you need to do a temporary redirect. The HTTP status code is 302.
header('Location: http://www.mysite.com/index.php?id=1', true, 302);
Keep in mind that it's good practice to not only send headers back for redirects, but a HTTP/HTML BODY as well that is shipping human readable information where the new location is. Redirects are not to be expected to be automatically performed by the client.
Different ways to implement
Depending on the system you work on, setting a HTTP status header with PHP might differ. The code above is for a working PHP version. Stick to latest. However, if you can not and the server integration is broken you might to push a bit the limits and force around a bit:
# Manually sending the HTTP 1/1 status line header - PHP does this nowadays, so normally not needed. But if you need it, ensure it's the first header you send.
header ('HTTP/1.1 301 Moved Permanently');
# Same here, but some CGI/FCGI+PHP implementations require you to set the Status header as well manually. Normally not needed.
header ('Status: 301');
# Set the Location header and status: (you will always need this)
header ('Location: http://www.mysite.com/the-real-name.htm', true, 301);
Always check if your script sends the correct headers by requesting it with a tool that is able to display the response headers not performing the redirect automatically, like curl:
$ curl -i "http://www.mysite.com/index.php?id=1"
Otherwise it takes a little long to wait for google to reflect the changes only for you to realize that you made some error.
When redirecting also set a 301 header and the search engines will know what to to from there.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
If the relationship cannot change, use a permanent redirect as hakre suggested (a 301 status code). Otherwise, if that same id value might point somewhere else in the future, use a temporary redirect.
In either case, if the canonical (official, main, primary) URL is "http://www.mysite.com/the-real-name.htm", you can tell search engines that with a canonical meta tag in the page's head section:
<link rel="canonical" href="http://www.mysite.com/the-real-name.htm" />

Change referrer when redirecting

What I'm trying to do is use PHP to redirect from web site A to web site B (both are different domains), but I want the referrer in the HTTP headers to be set to web site A (the page that performed the redirect). So, that is, web site B will see web site A as a referrer.
You can use something like <meta http-equiv="refresh" content="1;url=http://siteb.net"> on your site A.
I'm pretty certain that the sending the referrer is ultimately up to the web browser, not PHP.
You could probably send the address via $_GET though.
Edit: You won't be able to change the referrer (misread the post, derp).
I've checked using localhost and a dummy script. The browser does send the referer in the HTTP even at redirections.
To test this, I created a script called testRefererRedirect.php:
<?php
if($_GET['a']){
if($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != 'off'){
echo $_SERVER['HTTP_REFERER'];
}else{
header('Location: https://localhost/testrefererredirect.php?a=1');
}
}else{
echo 'test';
}
To emulate cross domains, I used HTTP and HTTPS for my local server.
On first load, the page will show a link: I will click this link to allow the browser to send the referer in the headers. Next, because I load the page initially in HTTP the header function will be called. Finally, the HTTP referer header meant for the 2nd step showed up in the 3rd step.
Conclusion
You can safely use $_SERVER['HTTP_REFERER'] on website B to capture the refer information meant for website A if you do redirection on website A.

Categories