Solved

PHP $_REQUEST using an Array?

Posted on 2010-11-18
6
482 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
Comment Utility
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
Comment Utility
Also

extract($_SESSION);

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

Expert Comment

by:Dave Baldwin
Comment Utility
$_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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 13

Expert Comment

by:dsmile
Comment Utility
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
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

15 Experts available now in Live!

Get 1:1 Help Now