Solved

Query String to Session

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

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

Technology Partners: 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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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 …

730 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