?
Solved

Query String to Session

Posted on 2013-01-25
3
Medium Priority
?
299 Views
Last Modified: 2013-01-27
Hi Experts,

I'm not a PHP programmer, but rather a Classic ASP. I was hoping if someone can translate the below simple code in PHP, assuming its also simple in PHP:

If Request.QueryString("pc") <> "" Then
  Session("pCode") = QueryString("pc")
End If

Open in new window


Basically, if the query string, pc, has a value, copy it to the Session variable, pCode, otherwise don't update pCode.

Thank you.
0
Comment
Question by:APD_Toronto
[X]
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
3 Comments
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 668 total points
ID: 38819492
if ($_GET['pc'] <> ""):
  .$_SESSION['pCode'] = $_GET['pc']
endif;

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 664 total points
ID: 38822119
The default behaviour/configuration in php requires you to explicitly initiate the session "engine/mechanism" by first calling "session_start()" (preferable at the very top) on pages where you will be using(reading/setting) session variables - ex:
<?php
sesssion_start();
...
if( isset($_GET['pc']) && ''!=$_GET['pc'] )
{
  $_SESSION['pCode'] = $_GET['pc'];
}
...
?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 668 total points
ID: 38822627
Test cases (run in order to see the moving parts).
http://www.laprbass.com/RAY_temp_adp_toronto.php
http://www.laprbass.com/RAY_temp_adp_toronto.php?pc=ABC
http://www.laprbass.com/RAY_temp_adp_toronto.php
http://www.laprbass.com/RAY_temp_adp_toronto.php?pc=XYZ
http://www.laprbass.com/RAY_temp_adp_toronto.php?pc=

While it is possible to configure PHP to auto-start the session, it's rare in my experience to find that configuration option, so I second what hielo says about session_start().

This example shows the use of the PHP ternary operator to achieve the test-and-set behavior that is often used to assign default values.

<?php // RAY_temp_adp_toronto.php
error_reporting(E_ALL);


// YOU ALMOST CERTAINLY HAVE TO RUN THIS STATEMENT TO START THE SESSION HANDLER
session_start();


// USE THE TERNARY OPERATOR TO EVALUATE THE REQUEST AND SET THE SESSION
//       --- EXPRESSION ---            --- IF TRUE ---           --- IF NOT TRUE ---
$pcode = !empty($_GET['pc']) ? $_SESSION['pcode'] = $_GET['pc'] :      FALSE;


// VISUALIZE OUR WORK
if ($pcode)
{
    echo "SESSION UPDATED: ";
    var_dump($_SESSION);
}
else
{
    echo "SESSION NOT CHANGED: ";
    var_dump($_SESSION);
}

Open in new window

Best to all, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

777 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