Jump to content

Recommended Posts

Hello everyone,

 

I've needed to create a page with such security:

 

I give users a random number, and an operation ( y = 2x + 1 ), my program calculate y, with random number, and user also calculate him self and write the result, and if the user's result is ture, it goes to Calculate.php, It's just a small university project, so I've putted users in an array, in users.php. I think it's not so important,

 

the problem is that, I used $random = rand(1,10), then I putted it in a session to keep the number, and then put the session in another variable ($x), anyway!

 

My problem is when user click on sumbit, it's like refreshing the page, and generator or like array_rand, will generate antoher number, so I get the result as calculation is not right,

 

any help to save the number from generator to compare with user's answer?

 

and here is my code, with array_rand:

 

<?php
session_start();
$x= 1;
$y = 2 * $x + 1;
$my_array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        for ($i=0; $i<=10; $i++)
        {
            $random = array_rand($my_array);
                        //this generates the random number from the array

        }
//Or simply $random = rand(1,10);
// $_SESSION['x']= $random;
// $x = $_SESSION['x'];        	

	$_SESSION['x']= $random;
			$x = $_SESSION['x'];
        echo "Our random number is: $random, "; // printing result
        echo "Our random number in session is: $x"; // printing result

if(isset($_POST['submit'])){
if (!ereg("^[A-Za-z0-9]", $_POST['username']))
	exit("<p>Invalid characters in the username.</p>");

$username = $_POST['username'];
$password = md5($_POST['password']);
$Cal = $_POST['number'];
require('users.php');
if (array_key_exists($username, $users)) {
	// at this point we know the username exists
	// let's compare the submitted password to value of the array key (the right password)
	if ($password == $users[$username]) {
		if ($Cal == $x) {
		// password is correct
		//session_start();

		$_SESSION['loggedin'] = md5($username.$password);
		$_SESSION['uname']= $username;
		header("Location: Calculate.php");
		exit; 
		}
		else {
		exit("<p>The Calculation is not right.click <a href='login.php'>here</a> to login again.</p>");
	}
	} else {
		exit("<p>Invalid password.click <a href='login.php'>here</a> to login again.</p>");
	}
} else {
	exit("<p>Invalid username.click <a href='login.php'>here</a> to login again.</p>");
}
}

echo"<center>Our Random X is <b>$x</b></center><br><br>";

?>


<html>
<head>
    <title>Login Form</title>
</head>
<body>
    <center><form method="post" action="login.php">
<table>
    <tr><td>Username:</td><td>
    <input type="text" name="username"></td></tr>
    <tr><td>Password:</td>
    <td><input type="password" name="password"><br></td>
<tr><td>Y = 2x + 1</td></tr>
<tr><td>Y = <input type="text" name="number"></td></tr>

    <tr><td><input type="submit" name="submit" value="Login"></td></tr>

    </form></center>
</body>
</html>

 

-------------------------------------------------------------------------------

On sumbit both $random & $x change to a new number!

 

        echo "Our random number is: $random, "; // printing result

        echo "Our random number in session is: $x"; // printing result

 

 

You always set the session x to random no matter what.

 

You need to test that if session['x'] isset and $_POST['submit'] isset then you do not do the number generator. If it is not, then you do the number generator.

 

 

 

You always set the session x to random no matter what.

 

You need to test that if session['x'] isset and $_POST['submit'] isset then you do not do the number generator. If it is not, then you do the number generator.

 

I just have to say WOW!!!

 

I searched so much, I asked so much and no one could help me!

But ur solution it was so easy! and it worked very well..

 

Thank you so much!

 

My program works! I added if isset also for session['x']. Thank you!

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.