Detect when link is clicked, open in new frame - php

I would like to create a basic URL rewrite using frames.
I don't have access to .htaccess to do mod_rewrite.
Is there a way using PHP, jQuery, JavaScript etc. to detect which URL has been clicked on and then open URL in new frame?
Ex: user clicks on /index.php?12345 it will open in framed window /pages/12345/index.html and if they click on /index.php?54321 URL will open in framed window /pages/54321/index.html

I don't think I really understand what you mean. Usually url rewrite works like this:
User clicks on http://example.com/content/example
Which is the rewritten to http://example.com/index.php?cat=content&page=example
You can somewhat fake this effect by making your links into http://example.com/index.php/content/example the webserver will still request the page index.php, in which you can then read the part after index.php (but before a query string) with
$_SERVER['PATH_INFO']
and then parse that to get what you need.
PHP.net on $_SERVER['PATH_INFO']
Contains any client-provided pathname
information trailing the actual script
filename but preceding the query
string, if available. For instance, if
the current script was accessed via
the URL
http://www.example.com/php/path_info.php/some/stuff?foo=bar,
then $_SERVER['PATH_INFO'] would
contain /some/stuff.

A PHP solution would be something along these lines:
if (!empty($_REQUEST)) {
$page = array_pop($_REQUEST);
if (is_numeric($page)) {
header("Location: pages/$page/index.html");
exit;
}
}

If I've really understand what you want, I think it's easy.
When the user clicks it calls a JQuery function which sends the content of the link to PHP with AJAX. After that, PHP analyses the link, gets the content of the page (with include()) and sends that to JQuery via JSON.

Such jquery might help,
jQuery(function($){
//this prevents all link elments' default behaviour
//when you click they won't navigate the page
$("a").click(function(){
//you get the links href
var actual_url = $(this).attr("href");
var new_url = "";
//[write your code here for converting actual url to new one]
//and open the new url in the frame you want
window.parent.yourFrameName.location = new_url;
//necessary for preventing default behaviour
return false;
});
});
Sinan.

Best solution is to use jquery to check if link was visited and then change link's target to _blank.
You could use plugin from this site:
link text and then execute such code:
$('a').visited(function () {
$(this).attr('target','_blank');
});
I think it is what are you looking for.

Related

Redirect a web page URL to another website

I have a simple PHP website. Let's call it youtubex.com. I want to redirect youtubex URLs (in the format shown on STEP2) to my website in the format shown on STEP3. Here, I am using YouTube, just for illustration.
STEP1: https://www.youtube.com/watch?v=lj62iuaKAhU
STEP2: https://www.youtubex.com/watch?v=lj62iuaKAhU
STEP3: https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
STEP1 shows any desired URL. STEP2 shows the same URL from STEP1 with youtubex as domain. STEP3 shows the final required URL. I am trying to redirect STEP2 to STEP3.
I tried finding some solutions to this on the internet and SO, but, none help. Here is one.
This should do the work:
RedirectMatch 301 /watch$ https://www.youtubex.com/#url=https://www.youtube.com/watch
An inefficient but full php solution can be using the location header in php :
$vid = $_GET['v']
if($vid){ header("location:https://www.youtubex.com/#url=https://www.youtube.com/watch?v=$vid");
}
by the way you can't get the text after the hash mark in php because it is not sent to the server.
javascript can do it in a more neat way without the second reload by checking window.location.href to see if the hash does not exist already and then get the v parameter in url then change url without refreshing the page by using window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
using php str_replace and header :
$step2_url = "https://www.youtubex.com/watch?v=lj62iuaKAhU";
$part2_url = str_replace("youtubex","youtube",$step2_url);//the output is : https://www.youtube.com/watch?v=lj62iuaKAhU
$step3_url = "https://www.youtubex.com/#url=".$part2_url; //the output is : https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
now you have the final url , simply redirect
header($step3_url);

How to use data-saferedirecturl in a tag in PHP?

I want any other server request goes through my server just like link in Gmail and i checked it there data-saferedirecturl="".
So how to use data-saferedirecturl in PHP automatcally in our website?
Exapmple:
Link
All links in Gmail are interpreted in the browser.
The data-saferedirecturl tag is added automatically.
So the href shows the link that you will be clicking in the bottom of your browser but sends you to a google-originated URL like https://www.google.com/url?hl=en-GB&site.com/324dd3.
This way the third party don't have access to sensitive data.
There are different ways to achieve this.
You can point all the redirects to the same page with the safe URL as a GET parameter (remember to use urlencode):
Link
Then in handler.php something like:
$desturl = isset($_GET["safeurl"]) ? $_GET["safeurl"] : false;
if($desturl != false){
//do something
header("location: ".$desturl);
}
If you want to use data-saferedirecturl you have to use some JavaScript, and intercat somehow with your server (ex. ajax calls).
Example (using jQuery):
HTML:
Link
JavaScript:
$(".test").click(function(e){
e.preventDefault();
let url = $(this).data("saferedirecturl");
// do ajax or check somehow the URL
location.href = url;
})

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

best way to determine whether a webpage is viewed in an iframe

Is there a way to determine whether the user is using a web page in side and iframe or is it normal browsing using PHP?
Using the javascript code that #deceze mentioned above (I pasted it in below),
if (parent.frames.length > 0) { ... }
If the above code noticed the page was displayed within iframe, then call 'IAmInIFRAME.php'(just example) via ajax call.
You can add some GET parameters to the request while using IFRAME.
<iframe src="http://www.example.com/iframe?iframe=1">
But while non-iframe request there wouldn't be this GET parameter.
You can check is this GET parameter presents and define it in the session.
So there would be different sessions for iframe and usual window.
The solution is to see if the parent's location and the current window's location is the same. If it is the same, then the page was loaded normally, if it is different then the page was loaded in an iframe.
var isInIFrame = (window.location != window.parent.location) ? true : false;
This came from this website. http://www.24hourapps.com/2009/01/check-if-page-is-loaded-in-iframe-using.html, and it came from the SO question here Check if site is inside iframe.
NOTE: In one test, I got a cross browser origin error but that would also only come if the two locations were different.

Current URL (from address bar) in a Bookmarklet

I'm building a bookmarklet and I need to get the current URL of the webpage the user is on when they activate the bookmarklet.
I tried using
$current_url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
But that will just get me the URL of the server where the JS for the bookmarklet is hosted. Is there anyway of getting the URL straight from the address bar?
You can resolve the problem by defining your SITE URL (eg: define('SITE_URL', 'http://abc.com')) and concat the site url with $_SERVER['REQUEST_URI']
Eg: $cur_url = SITE_URL.$_SERVER['REQUEST_URI'];
Yes, simply pass location.href form the bookmarklet.
For example;
location.href="http://mywebsite.com/bookie.php?url="+encodeURIComponent(location.href);
Then on your server, you get the URL at $_GET['url']. Good luck.
Of course, that's just JS. You need to add javascript: scheme for it to work.
If by bookmarklet you mean http://en.wikipedia.org/wiki/Bookmarklet than I think this code will help:
javascript:alert(document.location.href);
To test it, select the code and drag it in your bookmark browser bar than click it.

Categories