Solved

Passing data in $_Session vars

Posted on 2013-11-12
5
236 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I stared at the code and could not see it.
0
 
LVL 108

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

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.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

6 Experts available now in Live!

Get 1:1 Help Now