Solved

PHP $_REQUEST using an Array?

Posted on 2010-11-18
6
484 Views
Last Modified: 2012-05-10
Hi there,

I have an array of variables 100,101,102 - which are ID numbers.  I want to be able to call $_REQUEST["102"] - but using $_REQUEST["$_SESSION['ID'][2]"];  This bombs out, so I need to know the correct formatting for doing something like this.

Thanks!
0
Comment
Question by:maeve100
6 Comments
 
LVL 3

Expert Comment

by:roynaufal
ID: 34169385
you could try something like this instead:

$session_id = $_SESSION['ID'][2];
$_REQUEST[$session_id];


I also think this might be your problem:
$_REQUEST["$_SESSION['ID'][2]"];
should be:
$_REQUEST[$_SESSION['ID'][2]];

I personally use the 1st method though...I don't like too many brackets within each other, so it's nicer and cleaner
0
 
LVL 14

Expert Comment

by:ali_kayahan
ID: 34169421
Also

extract($_SESSION);

$_REQUEST[$id['2']];
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34169953
$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.  All of these are set by a page request from a form in a browser.  If the 'name' key you're asking for wasn't set by the page request, then it will cause an error.  A similar situation exists with $_SESSION variables.  If they are not already set, you will get an error trying to use them.

This $_REQUEST["$_SESSION['ID'][2]"]; tries to use the value of the $_SESSION variable as the name of the $_REQUEST variable.  Are you 'POST'ing the 'id's from a page?  Are the $_SESSION variables already set on a previous page?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:dsmile
ID: 34170479
If you want to get value from an array, you need to specify two things:
1. the array --> I can see it's $_REQUEST
2. the key (or what might create the key) --> don't know yet. Can you be more specific?
0
 

Author Comment

by:maeve100
ID: 34175750
roynaufal: $_REQUEST[$_SESSION['ID'][2]]; Does not work, the value of the request won't print - even though $_session['id'][2] prints the right value by itself.

dsmile: The $_REQUEST is a simple posted value of a single number.  The key/name of the hidden input is an ID number that I have stored in $_session['id'][1], $_session['id'][2], etc.

DaveBaldwin: You are correct that I am trying to use the session variable as the name of the request variable.  The REQUEST is a posted value of quantity, associated with the ID stored in the session variable.  The Session variables are set up previously in another page.

Thank you!
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 34175819
I would make it a two-step process (in part to make it easy for me to keep track).  I also just read on http://us2.php.net/manual/en/function.array.php that you are supposed use curly braces inside double quotes to access array variables.
// two-step
$idn2 = $_SESSION['ID'][2];
$item = $_REQUEST["$idn2"];

// original with curly braces
$_REQUEST["{$_SESSION['ID'][2]}"];

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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