Solved

PHP $_REQUEST using an Array?

Posted on 2010-11-18
6
496 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

695 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