Question: I am Setting session in one a.php like this 

```
<?php 
 session_start(); 
 $text = rand(10000,99999); 
 $_SESSION["vercode"] = $text; 

```

And accessing this session in another php form like this 

```
<script type="text/javascript">

    var Cpttext = "";

    function validate_test1(pricequote){

if(pricequote.vercode.value=='')
    {
       alert(Cpttext);
    alert("please enter correct captcha");
    pricequote.vercode.focus();
    return false;
    }
if(pricequote.vercode.value != Cpttext){
      alert(Cpttext);
    alert("please enter correct captcha");
    pricequote.vercode.focus();
    return false;
    }
}

</script>  

```

My captcha image code is 

```
 <tr>
 <td><label for="captcha">Enter Code:</label><img src="../include/captcha.php"></td>
 <td>
 <input type="text" name="vercode" />
 <td>
 </tr>

```

Intialised session in second form b.php like this 

```
 <?php @session_start(); ?>
<script type="text/javascript">
  Cpttext = "<?php echo htmlspecialchars(json_encode($_SESSION['vercode'])); ?>";
</script>`

```

but i am not getting the current session. i am getting the previous session. My aim is get current session and check whether it is correct or not 
Here captcha image showing one code session taking previous code value.how to get both same

Answer: 
You are setting the session key **"vercode" in a.php**, but in **b.php** you are echoing another session key **"versessioncode"**. Also you should be escaping generated javascript code.

```
<script type="text/javascript">
   var Cpttext = "<?php echo htmlspecialchars(json_encode($_SESSION['vercode'])); ?>";
</script>

```
You need to start session before use it:

```
<?php session_start(); ?>
<script type="text/javascript">
  Cpttext = "<?php echo $_SESSION['vercode'];?>";
</script>

```