UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, The GoNix Initiative, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
Sponsored Links
Latest Postings
Latest Reviews
Topic Options
Rate This Topic
#18686 - 10/13/05 04:18 AM problem with multidemensional arrays
Testing Offline
UGN Member

Registered: 09/21/05
Posts: 102
Loc: Sacramento, CA
I am currently working on a project from the book. It is a very simple form. However the script to handle the form simply isnt working and I cant figure it out. I swear I have checked and double checked my syntax over and over. I was hoping someone might be able to give it a peek and tell me where im messing up. Thanks in advance.

The Form

Code:
 <form action="handle_about.php" method="post">
<fieldset><legend>Enter your information in the form below </legend>


<b>Name:</b> <input type="text" name ="name" size="20" maxlength="40" />


<b>Interests:</b>
<input type="checkbox" name="interests[]" value="Music" /> Music
<input type="checkbox" name="interests[]" value="Books" /> Books
<input type="checkbox" name="interests[]" value="Skiing" /> Skiing
<input type="checkbox" name="interests[]" value="Napping" /> Napping

</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
</form> 
The script handling the form :

Code:
<?php 


if (strlen($_POST['name']) > 0) {
	$name = stripslashes($_POST['name']);
	
} else { // If no name was entered...
	$name = NULL;
	echo '

<b>You forgot to enter your name!</b>';
}

//Check $interests[]
if (isset($_POST['interests'])) {
	$ints = NULL; //New message to be used.
	foreach ($_POST['interests']) as $key => $value) { //loop through each.
		$ints .= "$value, ";

}
$ints = substr($ints, 0, -2);
$interests = TRUE;
		
} else {
	$interests = NULL;
	echo '

<b>You forgot to enter your interests!</b>';
	
	}
	
if ($name && $interests) {
echo "Thank you, <b>{$_POST['name']}</b>.  You entered interests as:
<tt>$ints</tt>";

}


?>  
All I get is a blank page.
_________________________
Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.

Top
Sponsored Links
      
#18687 - 10/13/05 05:31 PM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
Code:
 
<?
if(!$cmd){
//start form
?>
 <form action="index.php?cmd=send" method="post">
	<fieldset>
		<legend>
			Enter your information in the form below 
		</legend>
		<br/>
		<strong>
			Name:
		</strong>
			<input type="text" name ="myname" size="20" maxlength="40" />
		<br/>
		<strong>
			Interests:
		</strong>
			<input type="checkbox" name="interests[]" value="Music" /> Music
			<input type="checkbox" name="interests[]" value="Books" /> Books
			<input type="checkbox" name="interests[]" value="Skiing" /> Skiing
			<input type="checkbox" name="interests[]" value="Napping" /> Napping
		<br/>
	</fieldset>
	<div align="center">
			<input type="submit" name="submit" value="Submit Information" />
	</div>
</form> 
<?
//process form

}else if($cmd == "send"){
print_r($interests);
if (strlen($_POST['myname']) > 0) {
	$myname = stripslashes($_POST['myname']);
}else{ // If no name was entered...
	$myname = NULL;
	echo '

<b>You forgot to enter your name!</b>';
}
//Check $interests[]
if (isset($_POST['interests'])) {
	$ints = NULL; //New message to be used.
	foreach ($_POST['interests'] as $key => $value) {     //loop through each.
		$ints .= "$value, ";
	}
	$ints = substr($ints, 0, -2);
	$interests = TRUE;
}else{
	$interests = NULL;
	echo '

<b>You forgot to enter your interests!</b>';
}
if ($name && $interests) {
		echo "Thank you, <b>{$_POST['name']}</b>.  You entered interests as:
<tt>$ints</tt>";
	}
}
 
Try that. this is just an array not a multi demmensional array. I added the Print_r(); function on line 33 so you can see the array. you can add the pre tags to this to keep the formatting. Chec it out

Code:
echo "<pre>";
print_r($interests);
echo "</pre>";
Now your out put should look something like

Code:
Array
(
    [0] => Music
    [1] => Books
    [2] => Napping
)
  
_________________________
My New site OpenEyes

Top
#18688 - 10/13/05 08:48 PM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
Oh I forgot. You will see I put all the code on one page. I find this easier to keep track of later. a good way in my oppion to verify data came from a form is


Code:
<input type="hidden" value="<? $this_val = md5($_SERVER['REMOTE_ADDR']);  echo "$this_val"; ?>" name="secure_id">
then in the form processing script
Code:
$my_security = md5($_SERVER['REMOTE_ADDR']);
if($secure_id == "$my_security"){
     //do form processing here
}else{
     echo "Please use our form to submit data thank you";
}
For more security use crypt instead of MD5 which I belive to be machine specific.


What this dose is encrypts the users IP address with an MD5 hash. Then on the form Processing page we check the the value sent matches when we do it again. For dynamic IP addresses this will change often but should be fine to post the form data. But if they come back they will not be able to use the same MD5 hash to submit data without the form using the GET method.
_________________________
My New site OpenEyes

Top
#18689 - 10/13/05 11:24 PM Re: problem with multidemensional arrays
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7193
Loc: Portland, OR; USA
'eh I like to generate the secure_id with a combination of server ip or name and the users ip; as if the user's ip changes per page they're using a proxy swapper and I don't want to deal with them anyway ...
_________________________
Donate to UGN Security here.
UGN Security, Back of the Web, Elite Web Gamers & VNC Web Design Owner

Top
#18690 - 10/13/05 11:40 PM Re: problem with multidemensional arrays
KillHour Offline

*

Registered: 04/15/05
Posts: 247
Loc: Tonawanda, NY
That's not very considerate to us AOHellers, giz :p
_________________________
"We can categorically state that we have not released man-eating badgers into the area."

-UK military spokesman Major Mike Shearer

Top
#18691 - 10/14/05 04:14 AM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
Verizon now offers $14.95 DSL. Drop AOhell
_________________________
My New site OpenEyes

Top
#18692 - 10/14/05 05:36 AM Re: problem with multidemensional arrays
KillHour Offline

*

Registered: 04/15/05
Posts: 247
Loc: Tonawanda, NY
I'm not going through this again...

My IRC Logs With Inf.:

Quote:

[18:38] indeed
[18:38] hrm...
[18:38] aol...
[18:38] ...
[18:38] * Infinite thinks...
[18:39] the strange thing is
[18:39] that I HAVE broadband
[18:39] but my parents are too lazy to pick up the router
[18:39] so I'm stuck with dialup untill further notice
[18:39]
[18:39] how's that for funny?
[18:40] nice
[18:40] * shadow516 kicks wall
[18:40] ow
[18:41] hrm
[18:41] lets see if this works on you now...
[18:41] fuck
[18:42] I can tell what's comming
[18:42] * shadow516 cowers at inf's 1337 skillz
[18:43] omg it does
[18:43] hahahaha
[18:43] bye shadow
Session Close: Mon Oct 10 18:43:34 2005
Note to self: Things that start with "* shadow516 slaps Infinite - Take that bitch!" always end with "bash-3.00# ping -p 2b2b2b415448300d 172.131.40.24"
_________________________
"We can categorically state that we have not released man-eating badgers into the area."

-UK military spokesman Major Mike Shearer

Top
#18693 - 10/14/05 05:50 AM Re: problem with multidemensional arrays
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7193
Loc: Portland, OR; USA
Heh, get yourself a good wireless router (Linksys WRT54G or WRT54GS) ...
_________________________
Donate to UGN Security here.
UGN Security, Back of the Web, Elite Web Gamers & VNC Web Design Owner

Top
#18694 - 10/15/05 07:35 AM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
Hey Testing,

did this help at all? The code i posted works. It also makes it one page instead of 2.
_________________________
My New site OpenEyes

Top
#18695 - 10/15/05 08:34 AM Re: problem with multidemensional arrays
Testing Offline
UGN Member

Registered: 09/21/05
Posts: 102
Loc: Sacramento, CA
SORRY, Haven't worked on it today at all. Life has gotten in the way. Just got back from training and will check it tonight. Thanks for the help with this!
_________________________
Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.

Top
#18696 - 10/20/05 10:49 PM Re: problem with multidemensional arrays
Testing Offline
UGN Member

Registered: 09/21/05
Posts: 102
Loc: Sacramento, CA
Sorry it has taken me so long. Been absolutely slammed. I have checked out your script you made. Works perfectly!

It did help on the fact I can see the script work now however it doesn't help me figure out what I was doing wrong. Either way at least I can see the script in action. Granted im now able to basically understand whats gonna happen without running every single script! WOOHOO the learning has begun!!


So bonehead question # 2001 comming up. in your script you have (!$cmd)

What does the ! do?
_________________________
Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.

Top
#18697 - 10/21/05 12:18 AM Re: problem with multidemensional arrays
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7193
Loc: Portland, OR; USA
basically ! means "if no" thus, (!$cmd) is "if no command" or "if command is empty"; I use the same in some of my scripts to do basically the same as if($action == "") (so that'd be (!$action))
_________________________
Donate to UGN Security here.
UGN Security, Back of the Web, Elite Web Gamers & VNC Web Design Owner

Top
#18698 - 10/21/05 12:20 AM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
Not a bone headed question. I used this for about 10 scripts before I understood what was going on. lol It is funny how you can code something and truely have no clue how it works.


Code:
if(!$cmd){
}
The above code say basically this. If the variable $cmd has nothing set to it or does not exist. Do the code between the curly brackets { and }.

Code:
Line | Code
-----------------------------------
1    | if(!$cmd){
2    | }else if($cmd == "process_form"){
3    | }
You are now familar with Line 1. If $cmd has no value do what is inbetween { and }.

Line 2 we have
else if($cmd == "process_form"){

this is saying

else - $cmd has a value
if($cmd == - $cmd is a perfect match for (use 1 "=" for un perfect matches(not ideal for security))
"process_form" - the name that was passed in the $cmd variable.

and Line 3 I think you will get.

Now how do we set the $cmd value? Where did I get $cmd from?

First off $cmd can be anything. $cmd could easily be $deez_nuts or $action or anything you want it to be. To se the value you could use the form tag..
Code:
<form action="http://somesite.com/form.php?cmd=process_form" method="POST">
notice the "?cmd=process_form."

You could also use a hidden input tag

Code:
<input type="hidden" name="cmd" value="process_form">
or even a select element

Code:
<select name="cmd">
 <option value="process_form">Process option 1</option>
 <option value="process_form2">Process option 2</option>
 <option value="process_form3">Process option 3</option>
</select>
Now you can get reall tricking and have this also...


Code:
if(!$cmd){
   //provide a form
}else if($cmd == "process_form"){
   if(!$action){
        //do basic form process
   }else if($action == "advanced"){
       //do advance form process
   }//end action variables
}//end cmd variables
Just remeber to keep them nested and keep the code easy to read for yourself. Nothing suck more than trying to find that bug through 10,000 lines of code and it is all aligned left.
_________________________
My New site OpenEyes

Top
#18699 - 10/21/05 12:28 AM Re: problem with multidemensional arrays
§intå× Offline


*****

Registered: 12/03/02
Posts: 3255
Loc: Maryland
You sound like you have some ideas as to what is going on. Let me make a sugestion. Give yourself a basic idea. Then code it. For example...

form asking for - name - rant - email

that sends to a PHP processor that takes the data and put it to the screen. Play with it, tweek it. understand it the build on it. I think when you start with an idea you want to make happen you will really see it all click. and do not feel you are ever buging me in here. You got a free pass man, I will answer any question you have because i know you will do the research and apply the answer.

happy coding.
_________________________
My New site OpenEyes

Top

Moderator:  §intå×, Gremelin 
Featured Member
Registered: 10/28/14
Posts: 1
Forum Stats
2149 Members
46 Forums
35316 Topics
70486 Posts

Max Online: 1567 @ 04/25/10 05:20 AM
Top Posters
UGN Security 28477
Gremelin 7193
§intå× 3255
SilentRage 1273
Ice 1146
pergesu 1136
Infinite 1041
jonconley 955
Girlie 908
unreal 860
Newest Members
Tim050, Gecko666, defghi795767, Devo60, ali
2148 Registered Users
Who's Online
0 registered (), 368 Guests and 307 Spiders online.
Key: Admin, Global Mod, Mod
Latest News
luxury goods sales at $405B by 2019
by golqm
10/28/14 05:19 AM


Donate
  Get Firefox!
Get FireFox!