Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explode --problem

Status
Not open for further replies.

Elmserv

Programmer
Sep 25, 2004
72
GB
I have been using explode to break up session arrays & post arrays

the syntax is explode($_SESSION,10);

but its unreliable, what am I doing wrong

Richard
 
PHP Online Manual said:
Explode
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

I think you've misunderstood the function of explode, it doesn't separate arrays, it creates and array from a string by splitting it at pre determined characters.

so using explode on the following string:

$mystring="Section 1;Section2;Section3";
$mysections=explode(";",$mystring);

will yield and array containing 3 strings:

$mysections[0]-> "Section 1"
$mysections[1]-> "Section 2"
$mysections[2]-> "Section 3"

So what exactly do you want to do to the $_SESSIONS variable?



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi
I want the equivalent of
$password = $_SESSIONS['password'];
$username = $_SESSIONS['username'];

Rich
 
I'm not sure I understand,
Code:
$password = $_SESSION['password'];
$username = $_SESSION['username'];

It the correct way of addressing the session variables.
Do you want to put $_SESSION['password'] and $_SESSION['username'] into a string?



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
No
I use them to check if they are valid then continue.

obviously my use of explode has been incorrect & has really been working by luck. (not a recipe for reliable code)
Richard

 
So whats wrong with checking the $_SESSION directly:

Code:
if(isset($_SESSION['userame'])){
[green]//Do something if the session variable username is set.[/green]
}

or

Code:
if($_SESSION['username']=="myusername"){
[green]//Do something if the session variable username matches a string or another variable.[/green]
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Nothing at all, the $_POST variable has 40 elements and if I could expode that one it would save a lot of code.

Rich
 
You're looking for the extract function. It takes an associative array and creates variables in the current namespace based on the key names.

You should note that extract() does essentially the same thing as register_globals, and hence is can carry the same security risks. You shouldn't use it on untrusted data like $_POST, and if you do, then you should specify the EXTR_SKIP option, which will prevent extract() from overwriting existing variables.

As a side note, I really don't see why you need to do this in the first place. If you're just validating the data, it's just as easy to use the array keys. In fact, I find that it's actually easier, because you can loop through the array and share cases for the simpler fields (e.g. checking for valid dates, numbers, etc.)
 
If you just wanna loop through the $_POST, then try this:

Code:
foreach($_POST as $key => $value)
echo "$krey = $value<br />\n";
 
... sorry. Typo. It should read
Code:
foreach($_POST as $key => $value)
  echo "$key = $value<br />\n";

;-(
 
Thanks for the help, I will have a good look at the foreach command

Regards

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top