Solved

How to create a session onClick from a PHP Variable?

Posted on 2009-04-09
3
787 Views
Last Modified: 2012-05-06
I have a variable called $ItemName and I have a link to another page.  I want to store the $ItemName in a session when it is clicked.  The reason is because there are several $ItemName links on the same page.  So the current link needs to somehow click and create a session right before it goes to the page.

Can anyone help me out with this?
Thank you
0
Comment
Question by:JuniorBee
3 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 250 total points
ID: 24112330
why not just pass it as a $_GET variable?

echo "<a href='/next_page.php?item=".urlencode($ItemName)."'>Click me</a>";
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 24120237
I agree with Michael701 -- here is the general session handling strategy.

On EVERY PAGE at the top of the script put session_start();  I do that in a "config.php" file that I include as the first statement in every script I write.  By doing it every time, I know that the $_SESSION array is always available to my programs.  Because this is a superglobal array, it is available in all classes, methods and functions.

Then in "next_page.php" you would have code that looks like the snippet.

You might add some editing code to be sure that the URL string was acceptable for the rest of your processing.

HTH, ~Ray
<?php // RAY_get_to_session_example.php

error_reporting(E_ALL);
 

// ALWAYS START THE SESSION

session_start();
 

// COPY THE URL ARGUMENT INTO THE SESSION

$_SESSION["item"] = ''; // ASSUME NOTHING

if (!empty($_GET["item"]))

{

   $_SESSION["item"] = $_GET["item"];

}

Open in new window

0
 

Author Closing Comment

by:JuniorBee
ID: 31568746
Had to use javascript.
"ONCLICK"
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now