Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query String to Session

Posted on 2013-01-25
3
Medium Priority
?
300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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.
Suggested Courses

610 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