Is there any way in php to have a link so that when the user clicks on the link, they call a function and then immediately after calling the function, the user is redirected to another webpage.
$acceptEmailInvite; //append this url to the url in the link
if($app[UID]) {
//how do I have a link connect to two redirects?
$acceptEmailInvite = '/idx.php/store/accept/'.{$org['id']}; //link to this first
$acceptEmailInvite = 'getLink?org='.$D['bta']; //then link to this
}
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
EDIT: I meant that these two things happen only when the link is clicked. So the user should not be redirected unless the link is clicked. Sorry for the confusion.
Of course.
<?php
foo();
header("Location: http://example.com/");
exit();
The simplest way to do this is to have a page that performs the action, then redirects the user. For example:
/idx.php/organization/accept/
<?php
// Get your ID's, presumably from the URL
// Accept the invitation
acceptinvite($org['id']);
// Use header to redirect the user when you are done
header("Location: getBuzzed?org=".$D['betacode']);
exit();
?>
Your link would look like this:
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
.. but the user would see theirself go directly to getBuzzed....
You can see this functionality in action at Google or Facebook. If you google StackOverflow.com, the link in the results actually points here:
http://www.google.com/url?sa=t&rct=j&q=stackoverflow&source=web&cd=1&cad=rja&sqi=2&ved=0CCsQFjAA&url=http%3A%2F%2Fstackoverflow.com%2F&ei=ii-dUKaiMc_Psgad8oGYBA&usg=AFQjCNERidL9Hb6OvGW93_Y6MRj3aTdMVA
Google uses this page to track who has clicked on the link, and alert you if this link is dangerous.
Important
The page with the header command must not echo anything. If it shows any content, then you will get an error message when you try to use the header command.
Related
I have an application where URL after rewriting are like this
http://www.domain.com/product/seller/product_id
an example link would be
http://storeiown.com/product/kitchenking/92013
This was okay but I need the title of the product to be included in the url
http://storeiown.com/product/a-very-nice-electric-cooker-by-kitchenking/92013
I achieved this too and all was good.
Now, I want all the url which do not include the title to redirect to this one.
Like, if user lands from the url without the title they should be redirected to a version of the page with the url containing the title in the url.
How do i accomplish that. And for info additional info I use CodeIgniter in the app, if that makes it any easier.
you can do this way which i use. In the previous page type this at the top:-
<?php
session_start();
$_SESSION['mainpass']= '0';
?>
And in the next page code this at the top of the page :-
<?php
session_start;
if(isset($_SESSION['mainpass'])) {
//run the current page
}else{
header("location: www.domain.com");
}
?>
If you are using codeigniter, you could try the below.
$seg3 = $this->uri->segment(3);
if(is_numeric($seg3)){
//The user has come without the header because the third segment is numeric thus probably using the product id.
//Therefore, redirect again to the proper link after getting the heading from your db
} else {
// do nothing
// the seg3 is not numeric means it probably came through normal preferred way
}
And in order to use the
$this->uri->segment(3);
You need to either
auto load the url helper
load manually when required
im developing php and my webpage urls are like this
http://localhost/myweb/?action=news
http://localhost/myweb/?action=my_profile
http://localhost/myweb/?action=my_upload
http://localhost/myweb/?action=my_collection
http://localhost/myweb/?action=chat
http://localhost/myweb/?action=my_friends
etc
and for example, in particular page there's delete feature that access a href link
http://localhost/myweb/?action=my_friends&delete=2414535435 <-friends id
i do this to make it tidy by making the link cant be seen without looking at the source
a href='#'
and using javascript to access the real link
$('.deleteclass').on('click', function () {
var name= $(this).attr('nameattr');
if(confirm('You sure want to delete '+name+'?')){
window.location='?action=my_friends&delete='+this.id;
}
});
the problem is, after i process the delete and load the page, i don't want the full link on the address bar.
http://localhost/myweb/?action=my_friends&delete=2414535435
i need to make it back look like this on the address bar
http://localhost/myweb/?action=my_friends
is it possible? through mod rewrite perhaps?
you can not strip a url after the browser has loaded the page.
Just do a redirect after your delete or other action.
header('Location: /myweb/?action=my_friends');
Add error/success messages to a session variable (in the action script before doing the header) so you can show them on the other page
$_SESSION['errors'] = "Delete Failed: For Some Reason";
And on the other pages check to see if the session variable exists, show it and then remove it (otherwise it will stay there and the pages will continue to think there was an error)
<?php
if(isset($_SESSION['errors'])) {
//Do some code to show the error
...
unset($_SESSION['errors']); //delete the error messages
}
Well I am new to this so I want to record when the user clicks on the link that php prints and query a mysql database. I know how to query the database using php already but I'm not sure if it is possible to know if the user clicked on the link.
I printed a link like so.
print ('<a id="myLink" href="http://www.google.com" target="_blank">google</a>');
To track the link, you'd need to create a link tracking script on your server. i.e. linktracker.php
Then, change your code to point the link to that script, passing the forwarding url i.e
<a id="myLink" href="http://mysite.com/linktracker.php?url=http://www.google.com" target="_blank">google</a>
In linktracker.php, you would need something like:
<?php
$url = $_GET['url'];
// update your database click count for the url
// i.e UPDATE linkclicks SET clickcount = clickcount + 1 WHERE url = '$url'
// forward the user to the end location
header("Location: $url");
You need to build a URL redirection mechanism.
$link = 'http://www.google.com';
echo '<a href="/redir.php?target="'.encodeUriComponent($link).'>google</a>';
then make a redir.php:
<?php
$targetUrl = $_REQUEST['target'];
// log this targetUrl to your MySQL database.
header( 'Location:'.$targetUrl);
I absolutely wouldn't do this in JavaScript if you want to try to track links shares or something of that nature.
add onclick="handleClick()" and write javascript function named handleClick to report the click to the server uaing ajax
Unless the link is to your own site, you'll need to use a client-side scripting language such as JavaScript (could utilise jQuery too) to send the user's click event back to the server.
In CakePHP when you try to access a protect action you are automatically taken to the login form within your app. However the HTTP_REFERER is empty????
$referer = env('HTTP_REFERER');
echo $referer;
So normally when I visit the login page I will see the previous page URL with this code displayed, but if I visit the login page after being taken there by the Auth Component then it will be empty...
why is it empty? as I was just referred???
and how do I get it to acknowledge the redirect as a referal? NOTE: Using the Auth.Redirect session value is not an option in this case because it will stay around after the person has left the site, so for example if they return to the login page it will show the ORIGINAL requested action and NOT the referred page! So it will act as a referral even when its not because it's using the existing session
EDIT:
As an alternate example:
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}
else
{
echo 'their was no referer';
}
When the user is taken to the login form it will say their was no referer but that's obviously not TRUE! ????
HTTP_REFERER is a way of the browser to tell the server what page the user was visiting before. It does not signal what page the user has just been redirected from.
Take for example the Ads on Stackoverflow here. Clicking on one of them will take you to some long URL at adzerk.net, which records your click and then redirects you to the target URL. The intermediate Adzerk page is not an interesting page in itself and the user is never actually seeing it, unless he's paying close attention to the address bar. In fact there isn't even a "page" there. So it doesn't count as a "page visit". The next page will receive stackoverflow.com as the referer, the intermediate redirect page is irrelevant.
Stackoverflow -> Adzerk redirect -> Some advertiser
HTTP_REFERER: stackoverflow.com
There's also no referer at all if you type an address into the address bar. If you're on Stackoverflow and type yahoo.com into the address bar, Yahoo will not see any referer from you. If you click on a link that takes you from Stackoverflow to Yahoo, the browser does send a referer.
In your case, if you directly access a protected action by typing it in the address bar and get redirected, there simply is no previous page you came from.
As per the comments, here how to inject data into the URL while redirecting:
AppController extends Controller {
function redirect($url, $status = null, $exit = true) {
if (is_array($url)) {
$url['?'] = 'redirect=true';
} else {
$url.= '?redirect=true';
}
return parent::redirect($url, $status, $exit);
}
}
Is $_SERVER['HTTP_REFERER'] also empty?
If it is empty it is not set by the server, this means normally there was no referer, you did a simple refresh or called this page via bookmark. See some use cases for referer here: http://www.electrictoolbox.com/php-http-referer-variable/
Have you tried the refer controller?
$refer =
Controller::referer(); // referral url
How to link a HTML link like this - Click here to log out
to a PHP function like - logout()
What I need to do is, when people click a link, php will run the php function.
Please advice! :D
What I need to do is, when people
click a link, php will run the php
function.
You can not call a PHP (server-side language) function when clicking on a link. From your question, I suspect you want to provide a link for users to logout, below is how you should go about.
Your link should be like:
Click here to log out
And in logout.php you should put php code for logging the user out.
Code inside logout.php might look like this:
<?php
session_start();
unset($_SESSION['user']); // remove individual session var
session_destroy();
header('location: login.php'); // redirct to certain page now
?>
There are a number of ways; just to get this out of the way first. There is no way to invoke a PHP function from the client side (i.e, user interaction at the browser) without a page refresh unless you use AJAX. As one answer suggests, you can put the function in a PHP page, and link to it. Here's another way
Logout
And inside index.php
<?php
switch $_GET['action']:
{
....
case 'logout': logout(); break;
...
}
?>
There are other more sophisicated methods, such as determining which function to call from URI directly, which is used by frameworks like CodeIgniter and Kohana.