php - Session Doesnt work first time (want explanation )

361

Hello i am seen lot of question like Session doesn't working first time. But couldn't see any good explanation on this question and why doesn't working first time also what is happening during sessioning. Mine is also like others first time doesn't working after that works fine.

this is php which is sessioning.

session_start();

$PersonName=$_GET['PersonName'];
$SurName=$_GET['SurName'];
$TestXML=$_GET['TestXML'];
$TestDate=$_GET['TestDate'];
$TestPkID='0000000000000000000000000000';
include('DBConnect.php');

    $proc = "{call p_set_Test(?,?,?,?,?,?,?,?,?)}";   
    $params = array(array($TestDate,SQLSRV_PARAM_IN),
                    array(0,SQLSRV_PARAM_IN),
                    array($PersonName,SQLSRV_PARAM_IN),
                    array($SurName,SQLSRV_PARAM_IN),
                    array($TestXML,SQLSRV_PARAM_IN),
                    array('',SQLSRV_PARAM_IN),
                    array(101,SQLSRV_PARAM_IN),
                    array(10,SQLSRV_PARAM_IN),
                    array($TestPkID, SQLSRV_PARAM_OUT)  
                   );       
    $result = sqlsrv_query( $conn, $proc, $params); 
    if( $result === false )  
    {  
         echo "Error in executing statement 3.\n";  
         die( print_r( sqlsrv_errors(), true));  
         $message2 = "aldaatai";
            echo "<script type='text/javascript'>alert('$message2 ' + $TestPkID);</script>";
    } 

    $_SESSION['idpktestsession'] = $_POST["idpktest"] = $TestPkID;
    $_SESSION['persontestname'] =$_POST['persontrollname'] = $PersonName;
    $_SESSION['persontestlastname'] =$_POST['persontrolllastname'] = $SurName;

this is getting value from sessioned values

<?php include('DBConnect.php'); 
session_start();
    $diskuserid = $_SESSION['idpktestsession'];
    $diskusername = $_SESSION['persontestname'];
    $diskuserlastname = $_SESSION['persontestlastname'];
    $diskuserlastname = mb_substr($diskuserlastname, 0, 1);


    ?>
<?php   

                $proc = "{call p_rpt_Pattern(?,?)}";   
                $params = array($diskuserid,'M');  
                $procarr = array();
                $result = sqlsrv_query( $conn, $proc, $params);             
                while ($row = sqlsrv_fetch_array($result))
                {?>
                    <tr>
                        <td><?php echo $row['PatternCode']?></td>
                        <td><i class="fa fa-chevron-right rightarrow" > </i></td>
                        <td><?php echo $row['PatternDesc']?></td>
                    </tr>
            <?php
                }
            ?>  
216

Answer

Solution:

This statement was written incorrect:

$_SESSION['idpktestsession'] = $_POST['idpktest'] = $TestPkID;

If you want to assign the value you can rewrite above statement like this

$_SESSION['idpktestsession'] = $_POST['idpktest'];

and if you want to concatenate two values you can rewrite like this

$_SESSION['idpktestsession'] = $_POST['idpktest'] . ','. $TestPkID;
557

Answer

Solution:

As long this is question wasn't answered i found why it don't work first time. Everytime it sending session to another php with value ,its sending with value after page load so if you some how face this error just assign value before page load or put middle loading php insteand of showing direct.

People are also looking for solutions to the problem: regex - By using PHP preg_match, how to get specific substring from string

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.