Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to create a session onClick from a PHP Variable?

Posted on 2009-04-09
Medium Priority
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
Question by:JuniorBee
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 19

Accepted Solution

Michael701 earned 750 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>";
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 750 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
$_SESSION["item"] = ''; // ASSUME NOTHING
if (!empty($_GET["item"]))
   $_SESSION["item"] = $_GET["item"];

Open in new window


Author Closing Comment

ID: 31568746
Had to use javascript.

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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