Skip to content
Advertisement

How to set a session variable when clicking a link

I have the following problem… I want to set a session variable when clicking on a normal link like:

JavaScript

My research seems to point out that it is not possible for PHP to catch up with the click event in a such a way that it would set a session variable.

I believe it is possible with Ajax, but how? And what would my link look like?
Setting the session variable should look like:

JavaScript

So in short: When clicking on a link in HTML, a session variable must be set.
HOW am i going to do that?

PS: I’m not quite familiar with Ajax, but I’ll catch up.

EDIT: The links will refer to the same page, also i want to avoid urls like “home.php?link=X”.
If it isn’t possible to do it any other way, too bad. But I’ll hope there is a solution.
Important: the name of the link will be the value of $_SESSION['link']

Advertisement

Answer

JavaScript

Maybe this is what you’re looking for? It will remember the old link/page you’re coming from (within your website).

Put that piece on top of each page.

If you want to make it ‘refresh proof’ you can add another check:

JavaScript

This will make the page not remember itself.

UPDATE: Almost the same as @Brandon though… Just use a php variable, I know this looks like a security risk, but when done correct it isn’t.

JavaScript

PHP:

JavaScript

Why even store the GET in a session? Just use it. Please tell me why you do not want to use GET. « Validate for more security. I maybe can help you with a better script.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement