Solved

Query String to Session

Posted on 2013-01-25
3
293 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
3 Comments
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 167 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 166 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 108

Accepted Solution

by:
Ray Paseur earned 167 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

21 Experts available now in Live!

Get 1:1 Help Now