Here $a and $ b are equal. But in line 07 its not printing 'this is equal'. What is the problem? Plz help me. Remember when form submit $a and $b is equal. but when refresh they r not equal.

<?php
session_start();
$a=$_SESSION['randomnum'];
$b=$_POST['secure'];
 echo $a.'<br/>'.$b;
if($a==$b){
  echo "this is equal";
}
$_SESSION['randomnum']=md5(time());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#textarea{
    width:90%;
    border:thin #F00 solid;
}
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>&nbsp;</p>
  <table width="600" border="0" align="center" style="text-align:center">
    <tr>
      <td width="54">Question</td>
      <td width="536"><textarea name="question" rows="10" id="textarea"></textarea></td>
    </tr>
    <tr>
      <td colspan="2"><input type="hidden" name="secure"  value="
      <?php
      echo $_SESSION['randomnum'];
      ?>" />        
      <input type="submit" name="button" id="button" value="Submit" /></td>
    </tr>
  </table>
  <p>&nbsp;</p>
</form>
</body>
</html>

Try rearranging your codes like this

<?php
 session_start();
 ## if you want to predefined a session, then it should be here
 $_SESSION['randomnum']=md5(time());

 ## the above can be passed on to your form and then assigned as value of your variable $a..

 $a=$_SESSION['randomnum'];

 $b=$_POST['secure'];
 echo $a.'<br/>'.$b;
 if($a==$b){
 echo "this is equal";
 }

 ?>

Let me know if it work out for you, so that I can test it...

I think it will not be equal on submit and during page refresh, because the time will always be different as defined by your codes in

 $_SESSION['randomnum']=md5(time());

When the form is first submitted, the a and b are equal, but when your refresh the page, the randomnum will revalidate to the time of refresh, while the secure value in the form is not being revalidated. However, if you click on your browser's back button, both will be have the same value.. as long as you are not refreshing it is cool.

<?php
session_start();

if you want to predefined a session, then it should be here

$_SESSION['randomnum']=md5(time());

the above can be passed on to your form and then assigned as value of your variable $a..

$a=$_SESSION['randomnum'];
$b=$_POST['secure'];
echo $a.'<br/>'.$b;
if($a==$b){
echo "this is equal";
}
?>

No its not working

But when I use then its work. Actually I m trying to make a form submit that will not work at refresh

if($a!=$b){
  echo "this is equeal";
}

Yes, that would echo, but if yo want the session equal to your posted secure value, it does not..

Try this... create a new page this will be your entry page or pre-form page. Meaning everyone should pass through this page before they can even see your form.. in this page put these codes and save it as preform.php

 <?php


 session_start();
 $this_num = md5(time());
 $_SESSION['randomnum']= $this_num;

 echo "<a href ='form.php'>Go to the form</a><br/>".$this_num ;
 ?>

Create another page and save it as form.php

<?php

 session_start();
 ## if you want to predefined a session, then it should be here
 $this_session = $_SESSION['randomnum'];
 echo $this_session."<br/>";

 ## the above can be passed on to your form and then assigned as value of your variable $a..

 $a= $_SESSION['randomnum'];
 if (isset($_POST['submit'])){
 $b=$_POST['secure'];
 echo $a.'<br/>'.$b;
 if($a==$b){
 echo "<br/>this is equal";
 }
 }
else{
?>
<form method = "post" action = "">
<input type ="text" name = "secure" value ="<?=$_SESSION['randomnum']?>"/>
<input type = "submit" name = "submit" value = "submit" />
</form>      



<?php        
    }


?>

Direct your browser to the preform.php and click the link on it. That should take you the actual form. Submit the form, the secure value and the session should have been isolated in this page and have the same value..

Modify your codes base on these test pages...

<input type="hidden" name="secure" value="
<?php
echo $_SESSION['randomnum'];
?>" />

Is that putting a return/tabs into the value? try:

<input type="hidden" name="secure"  value="<?php
      echo $_SESSION['randomnum'];
      ?>" />

If not its probably getting converted in some way in the post, try changing

echo $a.'<br/>'.$b;

to

var_dump($a);
echo '<br/>';
var_dump($b);
echo '<br/>';

Then view the page source not the html, could be canonicalising in some way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.