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.
Related
id like to get the url of an website, which an website is automatically referring to.
For example:
When i browse the link "www.example.de" it refers me automatically to "www.example.de/example123/example.php" (This one id like to get)
Unfortunetaly it is not possible to get the second link but using the first link.
Greeting,
Geigerkind
Try this out:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(Sorry haven't got a PHP up to test on)
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.
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).
So I have a test version of my site. In the header of the test server, I'd like to include a link to same page on the live server.
Is there an HTML or PHP means of "knowing" what the current page is?
Click me!
or alternatively,
Click me!
With PHP you can use:
$_SERVER['REQUEST_URI'];
A hyperlink would look like this:
Click for current page
Note that PHP_SELF will show only the filename, and not the GET params.
If the url is like: index.php?page=aboutus
REQUEST_URI would be index.php?page=aboutus
while PHP_SELF would be index.php
Take a look at: http://php.net/manual/en/reserved.variables.server.php
There is JavaScript's window.location - Object, which makes many useful information available. There is also
$_SERVER['REQUEST_URI']
on the PHP side, as mentioned in the other answers.
Suppose that your test server is http://test.site.com, and your live server is http://site.com, then you could just use an absolute URL:
thepage.html on live server
In PHP, $_SERVER variables will get URL parts. In JavaScript, document.URL is the place to start. :)
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.