Jump to content

Recommended Posts

Hi,

 

I want to use php to do something similar to javascripts onclick function.  Basically I don't want to generate the link until a user clicks on it, and depending on what they clicked is what the url will be. 

 

My first thought was to have all links go to something like redirect.php, and use a session variable to track which link they had clicked then use metarefresh.... but that won't work unless there's a way to uniquely identify each link.... and I couldn't come up with anything. 

 

So if anyone has any suggestions, I'd be very grateful.  :)

 

You're thinking about PHP in the wrong way.

 

PHP is a server-side language. JavaScript is client-side. You're wording your question completely wrong so it's hard to know exactly what you want.. PHP is a preprocessor (hence the name, PHP Hypertext Preprocessor), it processes everything and then outputs the result to the browser. So it wouldn't handle anything done after the page has already loaded; so an onclick event in PHP essentially makes absolutely no sense.

 

As I said before I'm not completely sure what you want.. But you can echo JavaScript and have it be handled by the Browser..

 

echo "<a href='http://www.google.com' onclick='somefunction();'>Stuff</a>";

Yes, and I know javascript is what would be best for this task but I didn't think it would be a good idea to rely on JS because users can disable it. 

 

I guess I worded it very poorly before.  I'm trying to make a redirection script that dynamically selects the destination based on the source.  In other words, if the link meets certain parameters, it links to one place, if it meets other parameters, it link somewhere else, etc.  Preferrably using php because:

 

a) I want it to work for all users

b) I have other php scripts I would like it to work with

i have no idea what you're trying to accomplish either... you say you don't want to use js because users can disable it.  But you say that you want it to change based on what a link source is.  Well how is this link source being generated?  By js? If so, then you're already using js... with server-side scripting? Well the html output is coming from the server in the first place.  What part of your script is generating the html?

Sorry for the confusion.  I should have mentioned earlier that the main point is that I want to mask the link.  I've got so much crap I'm working on right now I can't keep it all straight.  I'm sorry.  *Slams head against wall*

 

Anyway, yes, the html output is being generated with php.  But if I just straight link then the destination url will be visible. 

 

I'd like to redirect with meta refresh so I can rel nofollow the redirect page and keep serp.  However what I'm trying to avoid is having a separate file on the server for each link.  Yet in order to make sure the link ultimately goes to the right place I need a way to track where it came from.  Also, http_referer won't really work in this case because there are many links on each page.

 

This is a bitch, I know.  :)

 

 

okay so basically you're going to have to have a unique id for each link, and instead of having the link point to the real url, have it point to a single page, with the id attached to the url, and on that page, forward the user to the real url based on that id.  Example:

 

pagea.php

<a href='controller.php?id=123'>link A</a>
<a href='controller.php?id=456'>link B</a>

 

controller.php

<?php
// example of id & link list. can hardcode it as array or store in a text file or db or whatever
$links = array('default' => 'someDefaultPage.php', '123' => 'realpageA.php', '456' => 'realpageB.php');

// get the real url based on the id passed. If it's not in the array, assign a default
$dest = (isset($links[$_GET['id']))? $links[$_GET['id'] : $links['default'];

// rediret user to real page
header("Location: $dest"); exit();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.