Solved

Passing data in $_Session vars

Posted on 2013-11-12
5
238 Views
Last Modified: 2013-11-15
I have a page (BuildingChk.php) that is used by several other pages.  Depending on the page that it came from, it passes data in $_SESSION['Data'].  

The page I'm having trouble with needs to add 2 variables to session variable.  I'm building the session variable like so:

$_SESSION['Data'] = $ROW2['SEQ'].$ROW2['NOR'].$ROW2['SOU'];

This does work.  

My problem is the length of "NOR" and "SOU".  They could be any where from 1 to 4 chars.

Is there a way to set them to 4 chars before building the string?

Also, the following code comes from the page that get's the results.

I've modified the code so that it is only looking for 2 chars in north and south (quick fix).

echo $_SESSION['Data']."<br>";
$strt = substr($_SESSION['Data'],0,9);
$work = $strt."%";
$north = substr($_SESSION['Data'],12,13);
$south = substr($_SESSION['Data'],14,15);
echo $strt."<br>";
echo $work."<br>";
echo $north."<br>";
echo $south."<br>";

The results that I'm getting from the "echo" statements are as follows:
NY10350005755251
NY1035000
NY1035000%
5251
51

Everything is correct except for the $north.  Why is it returning 4 chars instead of 2?

Thanks
Glenn
0
Comment
Question by:breeze351
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39643500
Because the second number is supposed to be the length, not the ending position.
$north = substr($_SESSION['Data'],12,2);
$south = substr($_SESSION['Data'],14,2);

Open in new window

http://us3.php.net/manual/en/function.substr.php
0
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 39643568
Is there any reason you can't simply make $_SESSION['Data'] an array?
$_SESSION['Data'] = array(
    'north' => '...',
    'south' => '...',
    'other' => '...',
    'stuff' => '...
);

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39644830
The PHP session is an array.  It can have as many data elements as you want.  Clogging them up together is a sure way to become confused!

Here is something like what I would recommend.  (I would also avoid the uppercase variable names.)

// NOT THIS: $_SESSION['Data'] = $ROW2['SEQ'].$ROW2['NOR'].$ROW2['SOU'];
$_SESSION['seq'] = $ROW2['SEQ'];
$_SESSION['nor'] = $ROW2['NOR'];
$_SESSION['sou'] = $ROW2['SOU'];

Open in new window

0
 

Author Closing Comment

by:breeze351
ID: 39651969
I stared at the code and could not see it.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39651985
We've all done that!  Welcome to the club :-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

861 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

23 Experts available now in Live!

Get 1:1 Help Now