php scriptable web browser - php

I want a url redirect tracer function in the php such as http://www.wheregoes.com/ .
I want to detect four kinds of redirects:
Http 301 redirect
Http 302 redirect
meta tag redirect
javascript redirect
If i use curl, i can easily detect 301, 302 redirect, but it is difficult to detect the other two redirections.
So i want a scriptable web browser, i will use a library as below:
$browser = Browser::createBrowser('chrome');
$delay = 10; // (This is a important parameter for detecting javascript or meta tag redirection).
$browser->load($url, $delay, function onLoadComplete($arr_track_url){
print_r($arr_track_url);
});
I searched and ran into some libraries such as http://www.simpletest.org/en/browser_documentation.html, but they don't support javascript and meta tag redirect.
Is there any php scriptable browser? Or can i detect javascript or meta tag redirection easily?

If I get that right you want to find out where some link finally leads to, if that final url differs from the url actually clicked in the first place?
If so I think the best approach is to let the browser do its work and loko afterwards where it came out. This way you get exactly the 'normal' behaviour of a browser, not that of some library.
Use a (hidden) iframe where you load the url into. Then wait for a window.load event or something and query the documents location afterwards. I didn't try that now, but sounds doable to me...

Related

auto refresh page with different URL

I am using following code for a refreshing page, it is not reloading on completion. The following code is not working sometime.
$url = array("www.yahoo.com","www.gmail.com");
$go=$url[0];
header("Refresh: 10; URL=$go");
"www.yahoo.com" may work in most browsers, but it's not a proper URL without the scheme.
Try http://www.yahoo.com/ etc. instead.
Additionally, redirects via header() are usually done with a Location: header, not a non-standard Refresh:. A refresh is best supported as a meta tag, not a header.

Laravel Check Back to Site

I want to check redirect to another link from our webpage if user clicking on back from browser I must be alert for user such as 'Backword Forbidden ...'
I'm using this code and that not working for me:
$referer = Request::header('referer');
or how to check witch URL user backword to our site?
If you want to get the Referer URL, you can use either Request::header('referer') or native $_SERVER["HTTP_REFERER"]. But there are (at least) 2 problems with that:
It can be spoofed, empty etc.
It will only work if the person got to your page through a link. It won't work when pressing the browser's back button or backspace.
The function you're looking for is Request::server() which functions just like the $_SERVER super global, so to get the page referer you'd do the following.
$referer = Request::server('HTTP_REFERER');
Using Request::header('refer') will only work for POST requests.
GET requests are the one your're looking for.
You can use Request::segment(1) or Request::segment(2), depends on the exact URL you're using.

window.location (JS) vs header() (PHP) for redirection

using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter

Detect and change external links in PHP?

I have a free PHP webserver and I would like to provide a redirect to external links page, just like deviantart.com does. Is there any way to do this with just PHP? I have no access to the server.
Edit: I meant a page asking "Are you sure you want to leave [MA WEBSITE]? NOPE ; DUH - GO TO http://outside-example.com"
Edit2: I actually meant a function to catch outside links and replace them with a /redirect/?url=PARSED_URL_ADDRESS
You need to detect if there is any link which redirects to outside website then you need a page to show something like "Now Leaving yourwebsite.com"
If that is the case then you need to analyze the content of your page before rendering and find out if there is any tags and replace ref of them with some gatway.php?url=outgoing-url
Where in gateway.php compare if the url belongs to your website or external website by using string comparison methods
Use this js code in footer (I am expecting there is some common footer page)
var urls = document.getElementsByTagName("a");
for (urlIndex in urls ) {
urls[urlIndex].href = "dummy.php?url="+urls[urlIndex].href; //replace dummy.php with urs
}
You mean like header('Location: http://www.example.com/');?
Provide, for example, a function that creates <a> tags. Or just one that converts URLs to your redirector: redirect.php?url=http://.... The redirector then issues a HTTP header called "refresh" set to the new address.. Beautify it so the user knows he is being redirected, voilá.
Find out yourself how :)
The best way to do it is using the location header, but you also need to set a 301 response code, this also tells the search engines crawling the link that the content at that url is at a different location, and it's a best practice to set the response code for redirects in general.
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );

Proxifying pages that use javascript

I'm building a proxy and am trying to deal with a page that uses javascript. The page has a button like this:
<input type="submit" ...cut this out... onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(...cut this out...)) />
When I click this button from my proxy the URL is rewritten to look like this (notice the javascript code inserted here):
http://domain.com/proxy/index-new.php?q=https://proxiedomain.com/javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(...cut this out...))
I'm not sure how I can handle this in my proxy server. When I don't use a proxy the headers are sent to a completely different page (the URL doesn't include this javascript). Can anyone give me any hints as to what I should look into or read to understand this problem better? From what I understand so far, I need this javascript to be executed (which would require a cient browser).
Any link that points to javascript:... will run JavaScript but not necessarily load a page.
I would leave these links alone, and instead ensure that the form action URL is set to your proxy, and any location.href = 'http://www.example.com/fully_qualified_urls'; are swapped for the proxy URL.
e.g. a simple RegEx replace of "OLD_URL" for "NEW_URL" (accounting for any HTTP vs. HTTPS protocol differences) should suffice for the most part.
Note: I'm aware it isn't "simple", but trying to inspect a javascript: based "link" to modify its behavior will be very awkward.

Categories