php random value - can't verify

Status
Not open for further replies.

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
i have no idea why, but this isn't working:

PHP:
<?php
$sec = rand(100000,999999);

echo $sec;

if ($_POST['check_field'] == $sec) {
    yes, do something
} else {
    no, throw error
}

For some reason it just keeps returning my error, even though the text in my html fieldbox is the exact value of $sec
 

Marcel

You're the guy who stole my car
Jul 17, 2015
466
208
i have no idea why, but this isn't working:

PHP:
<?php
$sec = rand(100000,999999);

echo $sec;

if ($_POST['check_field'] == $sec) {
    yes, do something
} else {
    no, throw error
}

For some reason it just keeps returning my error, even though the text in my html fieldbox is the exact value of $sec
I can't get it for some reason, seems perfectly fine to me.
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,934
3,933
I assume you are trying to create some sort of CSRF protection, and that you're putting the value of "$sec" in "check_field", and posting it?

If that's the case, this obviously won't work because the value of "$sec" is being regenerated every time you make a request. The solution would be to set "$sec" to a session, and then on POST check if the value is the same as the one that's stored as a session, and then set the new value for the session ($sec) below that.

Example:
PHP:
<?php
session_start();

$sec = rand(100000,999999);

echo $sec;

if (isset($_SESSION['sec']) && $_POST['check_field'] == $_SESSION['sec']) {
    yes, do something
} else {
    no, throw error
}

$_SESSION['sec'] = $sec;
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
I assume you are trying to create some sort of CSRF protection, and that you're putting the value of "$sec" in "check_field", and posting it?

If that's the case, this obviously won't work because the value of "$sec" is being regenerated every time you make a request. The solution would be to set "$sec" to a session, and then on POST check if the value is the same as the one that's stored as a session, and then set the new value for the session ($sec) below that.

Example:
PHP:
<?php
session_start();

$sec = rand(100000,999999);

echo $sec;

if (isset($_SESSION['sec']) && $_POST['check_field'] == $_SESSION['sec']) {
    yes, do something
} else {
    no, throw error
}

$_SESSION['sec'] = $sec;
Cool, i'll try this. thanks for the help, man.
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
It was still throwing a me my error for some reason, but i'm just going to assume it's some bug with my users class.
Anyways I'll consider this solved because i'll be going with recaptcha anyways :up:
Okay man, I'll close the thread. If you want to resolve the issue, just send over a PM and I'll re-open the thread again.
 
Status
Not open for further replies.

Users who are viewing this thread

Top