Forum Moderators: coopster

Message Too Old, No Replies

register globals!

         

ayushchd

12:50 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



The following code works well when register_globals is turned on bt unfortunately my host has it turned off. When i run the following code wid register_globals turned off...it is not able to check whether the user has entered the correct image or code or not and as a result even when the correct code is entered, "image verification failed is echoed"

<? session_start();
include ('mysql.php');

$user = $_POST['user'];
$actualpass = $_POST['pass'];
$pass = md5($_POST['pass']);
$email = $_POST['email'];
$bt = $_POST['submit'];
$time = base64_encode(time());
$random2 = trim($_POST['random']);
if ($bt) {
if ($new_string!= $random2) {
$notice = "<div id=\"failure\">Image Verification Failed.</div>";
} else {
$sql = "insert into users (username, password, initial_amount, email, timestamp) values ( '$user', '$pass', '200000', '$email', '$time');";
if (mysql_query($sql)) {
$notice = "<div id=\"success\">Click<a href=\"http://vsenepal/activate.php?user=$user&hash=$pass&id=$time\"> here </a> to activate. </div>";
} else {
echo mysql_error();
}}}

?>

<form name="form1" method="post" action="">

<td><div align="right">Enter the code as you see in the image below: <br> <font size="-2">Case Sensitive</font></div></td>
<td><input type="text" name="random"></td>

<td><img src="random.php"></td>

</form>

</body>
</html>
<?
$new_string;

session_register('new_string');

$im = ImageCreate(200, 25);

$white = ImageColorAllocate($im, 255, 126, 40);
$black = ImageColorAllocate($im, 212, 212, 212);
srand((double)microtime()*1000000);
$string = md5(rand(0,9999));
$new_string = substr($string, 17, 5);
ImageFill($im, 0, 0, $black);
ImageString($im, 10, 75, 5, $new_string, $white);

header('Content-type: image/png');
ImagePNG($im, "verify.png");
ImageDestroy($im);
session_destroy();
?>

[edited by: jatar_k at 1:04 pm (utc) on Aug. 3, 2007]
[edit reason] reduced code dump [/edit]

whoisgregg

1:06 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's actually a good thing your host turned off register_globals [php.net].

To update your script, just find every occurence of a variable that comes from a posted form and reference it using the correct superglobal ($_POST, $_GET, $_COOKIE, or $_SESSION).

// Instead of $new_string:
$_POST['new_string'];

Also, as your script is coded right now it is vulnerable to a sql injection attack [php.net]. Secure your script using mysql_real_escape_string [php.net]!

jatar_k

1:12 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



first thing, try to not dump all the code into your posts. Reduce the posted code to parts that are necessary. It is very difficult and time consuming for members to have to do all the debugging work for you and reduces the number of replies you get as people don't always have tons of time to debug.

secondly you need to approach this logically, start with what you know

register_globals was turned off and this script started giving an error

the error is
"image verification failed"

where does this error come from, easy enough to find

if ($new_string!= $random2) {

where do these vars come from? is it possible one of them was effected by the globals switch?

$random2

comes from here
$random2 = trim($_POST['random']);
which is posted from our form, it uses the proper POST syntax so it shouldn't have been effected

that leaves $new_string

where does it come from?

looks like this down near the bottom

$new_string;
session_register('new_string');

ahhh, SESSION, which is effected by globals, so maybe we can make a quick change and that will work

try using the full session syntax

if ($_SESSION['new_string']!= $random2) {

might work, if not then dump the session to the browser like so

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

then figure out what the session var you are looking for is called

then go from there

always identify the line/area of code that is causing the problem and then figure out where all the parts of that code are coming from and carry on

ayushchd

1:45 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



I tried what u said. Firstly, $_POST['new_string']; doesnt work, nor does $_SESSION['new_string'];

Then i tried :
echo '<pre>';
print_r($_SESSION);
echo '</pre>';

It said : Array
(
[new_string] => 320d2
)
But the number that was generated in the captcha image was not 320d2. IT was 180f4. And when in the text box i entered 320d2 i passed the verification but when i enter the captcha number it says "Image Verification Failed". What shud i do next? I also tried replacing $new_string with $_SESSION['new_string']. this din work either.

jatar_k

1:59 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



again why I dislike forms posting to themselves, all sorts of problems

I was going to test the script (the full one you originally posted) but it actually doesn't work at all for me

where did you put your echo statement for the session? I am guessing it is echo'ing the newly generated one

for testing do this

if ($new_string!= $random2) {
$notice = "<div id=\"failure\">Image Verification Failed.</div>";
echo '<p>new_string value: ',$new_string;
echo '<p><pre>session values: ';
print_r($_SESSION);
echo '</pre>';
die();

} else {

this will stop execution on error and dump some vars, you can then go back to regen and try again

ayushchd

2:25 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



hi jatar_k,
Your suggestions have helped me. Now I have finally pointed out the problem. If we just leave the SESSION aside for sometime and concentrate on $new_string.

I am sorry to paste a large chunk of code once again. But i need to do it to explain it to u.
What i did was defined $new_string at the beginning of the page. So the beginning was starting was this like this :

<? session_start();
include ('mysql.php');
session_register('new_string');
$im = ImageCreate(200, 25);
$white = ImageColorAllocate($im, 255, 126, 40);
$black = ImageColorAllocate($im, 212, 212, 212);
srand((double)microtime()*1000000);
$string = md5(rand(0,9999));
$new_string = substr($string, 17, 5);
echo $new_string;
and then the rest of the code like $user= $_POST['user'] and so on.

And i changed the check part to :

if ($new_string!= $random2) {
$notice = "<div id=\"failure\">Image Verification Failed.</div>";
echo " New string : $new_string";
echo " Input : $random2";
}

Before Submitting :

The output was : e3c5b (which was also the captcha code)

Then after entering e3c5b and submitting, the output changed to :

For the first echo of $new_string it was, ie, for this : $new_string = substr($string, 17, 5);
echo $new_string; The output was a newly generated random number : e0fa1

and for this part :

echo " New string : $new_string";
echo " Input : $random2";

The output was :
New string : e0fa1 Input : e3c5b

I think that the problem lies with the var $new_string. Could you please solve it for me?

jatar_k

2:29 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> What i did was defined $new_string at the beginning of the page. So the beginning was starting was this like this :

you broke it, the value is regenerated so you've changed what you are trying to compare

you have to keep that code at the bottom, I would think, as it is you are corrupting your own data

move it back to the bottom and try again

the code posted above is acceptable it's just when you keep dumping the whole thing intot he post that it is difficult for people to debug

ayushchd

2:36 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



On bringing it back to bottom, the output pattern remains the same as i told in my last post except one thing it outputs nothing for New String, it says :

New String:
Input: whateva i input

jatar_k

2:40 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's because you aren't doing exactly what I posted and your result is weird ;)

echo " New string : $new_string";
echo " Input : $random2";

you can't echo $new_string as it is from the SESSION try

echo " New string : ", $_SESSION['new_string'];
echo " Input : $random2";

I have also been wondering about the session_destroy(); at the bottom of the original script, doesn't really make sense that it is there, though I have not run and debugged this myself so it may have some use

ayushchd

2:45 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



How is $_SESSION['new_string'] related to $new_string?

jatar_k

2:49 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you need to understand where $new_string comes from

well $new string is set when you first load the page

that string has to be passed to the second page somehow, otherwise it would be impossible to compare what you entered into the box, how would the file you posted to have an idea what it generated previously?

well, by passing the original string used in the image via the session

you then compare the string typed into the textbox from the POST array to the value written into the SESSION

unless it is being passed in some other magical way that I am unaware of

I'm not totally sure how this script worked when globals was on, though it could have always worked and not properly failed

again, this type of confusion doesn't come up when form don't post to themselves

ayushchd

3:01 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



Hi, it worked.

But it works only for the first time when the session is newly set. I mean that the session does not destroy. Where shud i place session_destroy () ;?

I tried to place it at the end, it dint work. I think I have to place it somewhere between the validation checks.

jatar_k

3:07 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could change this line for one
session_register('new_string');
to
$_SESSION['new_string'] = '';

then a little farther down

change
$new_string = substr($string, 17, 5);
to
$_SESSION['new_string'] = substr($string, 17, 5);

and change
ImageString($im, 10, 75, 5, $new_string, $white);
to
ImageString($im, 10, 75, 5, $_SESSION['new_string'], $white);

this should make it all work, to be honest I still don't think it should be working as is

then just don't worry about the destroy at all

ayushchd

3:09 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



Why do you think it shouldnt work?

jatar_k

3:11 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because it's a big mix of globals on and off code, though I could be missing something

ayushchd

3:21 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



What u said, doesnt work...i think ill have to use session_destroy();

jatar_k

3:24 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



destroying the session isn't going to help, it just needs to be unset, if you destroy it then it will only ever work once

ayushchd

3:26 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



When shud i unset it?

jatar_k

3:30 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would think that once you've done the comparison you no longer need it

ayushchd

3:32 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



if i dont unset the session, it'll work only once!

jatar_k

3:34 pm on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright then do whatever you like, I am obviously not understanding something