Detect and change external links in PHP? - 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" );

Related

php scriptable web browser

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...

How to hide HTML Page Source in php by detecting the URL

I do not care about people viewing my source code, however, I want Bots to avoid coming on to my site and getting through my security. I was hoping to disable page source viewing. To do this, I am using this code:
$url= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$needle = "view-source:";
if (strpos($url,$needle)) { echo "You can not see me";}
else {
//The rest of my index page
}
The objective here is that if someone tries to view my page source or if a bot tries to, that rather than being able to see it, the code will detect that the page URL is view-source:www.yoururl.com and will display a "Nice try" message in the source instead of the page source. The code above in theory should have worked, but didn't. Any other idea's to try and make this work?
This cannot be done, the HTML source code is passed to whoever requests it. You should probably redesign your captcha, as it is not secure from how you described it. Use session variables to store the data and to check against the submitted value on the form processor script.
you could use mod_rewrite and a permanent 301 redirect in your .htaccess to hide the ?captcha=xxxx part of your url, if it is your sole concern.

(A|B) testing Google Analytics, remove utm_expid from URL

Im new to this and im trying to rewrite URL so that utm_expid is hidden so if my url is:
http://www.myweb.com/?utm_expid=67183125-2
how would i make it so when user visits
myweb.com
it does not show utm_expid in url
Is this possible using PHP/JS?
NOTE: i cant use RUBY or any other languages except PHP/JS/HTML
There is a way. Just redirect the page to base url once the utm_expid=67183125-2 is got. ie,
if($_GET['utm_expid']) { //header to redirect to myweb.com }
Its a tricky way. Hope you are permitted to use it.
Just start a session and store value in session variable. you can regain it even page is re directed.
ie
<?php
session_start();
if($_GET['utm_expid']) {
$_SESSION['variable_name']=$_GET['utm_expid']
//header to redirect to myweb.com
}
?>
Let me add this Javascript trick that is server agnostic.
if (location.search.indexOf('utm_expid') > -1) {
history.replaceState('page', 'Title', '/')
}
I recommend you to place it at the end of the body.
If you wanted a clean URL (as you do for branding and manual sharing purposes), I'd script it so that you load a full page iFrame which loads the gA test queried URL. That way the user see s the clean URL in the address bar and still see the experiment.
You could use PHP to set up your index page (or any server side, or even client side script).

Redirect a user to an external site while linking to an internal page?

How can I redirect a user to an external site while linking to an internal page ?
I have seen examples like:
example.com/go/ksdjfksjdhfls
example.com/?go=http://www.new-example.com
... And many more...
How this is achieved in php ?
Does this have any pros/cons with regards to SEO ?
I don't see any benefit in this approach, but there are a few ways to achieve it. To do it with the GET query, you would simply need the following code:
HTML:
Google!
PHP:
if (filter_var($_GET['site'], FILTER_VALIDATE_URL)) {
header('Location: ' . $_GET['site']);
}
With the above example, it will actually take the user to that location, not to:
http://example.com/link.php?site=http://www.google.com
To achieve the url being "local" while pulling up a remote site, you'd either have to:
Mess with URL rewriting, which can get messy and I'm not sure will let you do the above
Retrieve the remote page via curl and display it, which may screw up links on the "remote" page
Use iframes and set the iframe to be the size of the page. Note that this last method, while least offensive, is recognized as a potential security breach known as 'clickjacking' since it's used to trick users into clicking on a link for one page which his hiding a malicious link to another site. Many servers and browsers are taking steps to avoid this (for instance, google does not allow iframing of its home page), so this may also reach dead ends.
So of the three server-side methods I can think up, one may or may not be possible, and is a pain. One will be crippled and put a heavy load on the server. The last is a known bad guy and is likely not to work for many cases.
So I'd just go with a redirect, and really, if you don't need the address bar to show the local URL, then I'd just have a direct link.
All of the raises the question: What are you hoping to accomplish?
put this is beginning before any output to browser
<?
header('location:example.com\index.php');
?>
Set up an index php file which sets the header location to the url in the get parameter.
example.com/?go=http://www.new-example.com :
// example.com/index.php
<?php
if (isset($_GET['go'])) {
$go = $_GET['go'];
header('Location: $go');
} // else if other commands
// else (no command) load regular page
?>
example.com/go/ksdjfksjdhfls :
// example.com/go/ksdjfksjdhfls/index.php
<?php
header('Location: http://someurl.com');
?>
I use .htaccess rules for this. No PHP needed.
i.e.
Redirect 307 /go/somewhere-else http://www.my-affiliate-link.com/
So visiting http://www.mywebsite.com/go/somewhere-else will redirect to http://www.my-affiliate-link.com/.
On my site, I use "nofollow" to tell the search engines not to follow the link. The 307 status code means "temporary redirect".
Click here!
example.com/?go=http://www.new-example.com
you can use iframe and set the src attribute to http://www.new-example.com
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<iframe src="http://www.new-example.com" width="100%" height="100%"></iframe>
</body>
</html>

PHP: Best redirect technique

I'm currently using the excellent mobile detection script from: detectmobilebrowsers.mobi
This works really well however, it redirects every and any page on your main site (including any query parameters) to your mobile site's home page.
What I need is:
http://www.mydomain.com/page.php?var1=X&var2=Y
to direct to:
http://mobile.mydomain.com/page.php?var1=X&var2=Y
I have multiple pages that should redirect with the query string to their mobile versions.
What's the best way to approach this? I thought that I should:
Examine the $_SERVER['HTTP_REFERER'] for the page and query string, use a switch/case to loop through the 10 or so pages that I need matching on the main and mobile sites then change the referer URL in the mobile detection script.
Does this make sense?
I've been struggling to get the page and query... any advise and thoughts welcome.
if ($mobile_is_detected) {
header('Location: http://mobile.mydomain.com' . $_SERVER['REQUEST_URI']);
exit;
}
In addition to Andy's answer, when redirecting you should set the response status to 301.
Be careful, you may not call header() if you have printed any HTML or echoed anything before calling the function.
if ($mobile_is_detected) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://mobile.mydomain.com' . $_SERVER['REQUEST_URI']);
}
You can use $_SERVER['QUERY_STRING'] to redirect to add the query string to the redirect URL in the first place.

Categories