Solved

Query String to Session

Posted on 2013-01-25
3
292 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

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 …

760 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

20 Experts available now in Live!

Get 1:1 Help Now