Masking urls in javascript - php

I have a url1.
But I have seen on websites that they would use javascript to show this as url1 but it is actually url1 which redirects to url2.
You would not see the original url when you hover your mouse over the link.
The same thing is also used on google's search results. Can you please suggest how it is done. :)

You can use JavaScript onclick event to change the href on click.
Example: link
In this example users see that the link url is url1 but when they click on it goes to url2.

For a simple, example, you could make a redirect script that just takes the url out of a GET parameter, and then redirects the user to that page.
redirect.php:
<?php
$Link = $_GET['l'];
// Do whatever you want with $Link here (store it in a database, for example)
header('Location: ' . $Link);
?>
Now, instead of linking to url1, you can link to redirect.php?l=url1.
Please keep in mind that in an actual environment, you'd probably want to do some sanity checks on $Link before redirecting the user there.

Related

Wordpress: check if last visited page was homepage?

I'm fairly new to Wordpress, and really need some help on the PHP bit. On my wordpress pages (Let's call it Page A), I want to call a jQuery animation, but only if the visitor clicked a from my wordpress home page to get to Page A.
So just to be clear, how can I call a jQuery animation on page A given that the previous page was the homepage?
I wanted to use is_home() to test the page the visitor came from. But as far as I understand, is_home() can only be used to test if the current page is the homepage.
Is there a way to get around this?
You can use History object it has back method and you can check if it returns URL of your homepage
The other option is to use cookies on all your pages
$.cookie("previousPage", window.location.href, {path:"/"});
and then check on PageA
if($.cookie("previousPage") == 'yourhomepage') {
//call animation
}
notice:
$.cookie is available if you use jquery cookie plugin which can be found here
useful links:
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
https://developer.mozilla.org/pl/docs/Web/API/Document/referrer
https://github.com/carhartl/jquery-cookie
The WordPress PHP function get_home_url() gives you the URL of your home page (see documentation). Then in the JavaScript you can use document.referrer to compare with:
var home_page = '<?php echo get_home_url(); ?>';
if(home_page == document.referrer) {
//Animate with jQuery, or whatever it is you want to do.
}
You can also get the previous page in PHP using $_SERVER['HTTP_REFERER']. Wheter you use JavaScript or PHP the information might not be reliable since it comes from the browser. But if it ain't important that it's right every single time it is good enough.
Please note that this won't work if you for some reason have single quotes (') in your homepage URL.

Change Location of Outgoing Link On Click

I'm trying to link to another page without displaying the real URL on hover or in the source code. I want <a> to redirect to $newUrl; even if there is another url defined in the href="".
<?php $newUrl = "http://therealurl.com/folder/private.php" ?>
Link
So using the code above I want to replace http://domain.com with <?php echo $newUrl; ?> when the user clicks on the <a> because i never want it to be displayed to the user.
Is there a solution to target a specific anchor to do this? If not then every link on the page is fine I just want to do this in a way that the real URL is not displayed for privacy reasons.
Here is solution.
<?php $newUrl = "http://therealurl.com/folder/private.php"; ?>
<script>
$( document ).ready(function(){
$( '#link' ).click(function(){
var newLink = <?php echo json_encode($newUrl);?>;
$(this).attr("href", newLink);
});
});
</script>
<a id="link" href="http://domain.com">Link</a>
Don't forget to include jquery library.
If you have full access to the original link, you could use it as a redirect by setting a header in the destination script.
header("Location: yoururl.php);
This is the only quick easy solution that I can think of, as this one will not show the actual destination in the source code.
If you want to hide the link from the source code then any javascript methods won't work, to my knowledge.
You could also use ?page=get stuff to change where it sends the user, in order to make this script usable in multiple ways, just don't forget to manage your GET data safely.

url change without anchor tag and refresh using ajax

As per my client need , redirect the page without anchor tag and refresh page ,
and change the URL as per page appearance. I don't know any idea about this.
I can use ajax for page content. but I don't know the URL change option without
page refresh or redirect. how can I do this with ajax j-query. Any one guide me for this issue.thanks advance
sample url
www.samplesite.com/contact.php -> without anchor tag. and page refresh this url need to work on php.
I think you are looking for info about the new HTML5 History API ( pushstate ), this link has a detailed tutorial.
http://diveintohtml5.info/history.html
You can do this using below function.
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
You can use the following javascript functions
window.location.assign('http://www.samplesite.com/contact.php'); // keeps the current page in browser history
window.location.replace('http://www.samplesite.com/contact.php'); // replaces the current page in browser history
window.location = 'http://www.samplesite.com/contact.php'; // the same as assign() and is backward compatible even with the oldest browsers
jsfiddle

how can i redirect a user to custom url after adding page through add.php (Facebook)

How can I redirect a user to just added page after adding it through add.php?
let's say my url looks like: http://www.facebook.com/add.php?api_key=API_KEY&pages=1
After selecting a page from the select box and clicking 'Add MY_APP_NAME' user is redirected to the wall of the page instead of to freshly added MY_APP.
Notice
I know this type of questions lots of here but i have no found any solution that why i put this question here again.... helps are definitely appreciated
Tried
I tried myself like this using Javascript but seriously not success
<script>
if (window == top) {
top.location.href = 'https://apps.facebook.com/abc/' + document.location.href.replace(/https?:\/\/[^/]*\/?/, '');
}
function addToPage(page_id){
top.location.href = 'http://facebook.com/add.php?api_key=<?php echo C_APP_ID; ?>&pages&perms=publish_stream&page='+page_id;
}
</script>
See Image
https://www.facebook.com/dialog/pagetab?
app_id=APP_ID&
display=popup&
next=https://facebook.com
Using this alternative method you can specify the URL to be redirected to after interacting with the dialog to add an application to your page in the next parameter.
You could try adding that parameter to your request.
Other possible alternatives for the next parameter are -
redirect_url
redirect_uri
Both are used elsewhere when dealing with redirecting users - perhaps one of them will assist you as well...

How to make a window pop up from a link using javascript

I am making a bookmarklet for my url shortener. What it does is it shortens the current url of a page when you click on the bookmarklet, so you don't have to go to a site to shorten a url, but what I need is for the bookmarklet to return a pop-up window with the shortened url listed below.
What I've done so far is this:
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u;window.open(s,'shortened','location=no,width=400,height=300');
If you have bookmarked this, you will notice when you click on it, it does shorten the url of the current page, but it also shows the url shortener webpage, and what I only need is plain text and a link to copy it.
Please help!
Edit: Sorry I'm that bad at php, but how can I make a parameter "responseType"? Thanks
What is shown in the popup is what is returned by http://n1x.co.uk/create.php. You will have to modify the php to return only the shortened url in plain text. If the php is being used by the shortener site also, what you can do is pass a parameter to create.php that tells it whether to return the whole page like it does now or to return only the plain text url. For example a parameter called 'responseType'. You can modify the create.php to say that when responseType = 'plainurl', return simply the shortened url and not the whole page.
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u + '&responseType=plainurl';window.open(s,'shortened','location=no,width=400,height=300');

Categories