简体   繁体   中英

not working session_start() in my php code

I don't know why session_start() is not working after refreshing index.php.

The index.php page, should print "echo $_SESSION ['reg_code']; // Key Pass entered by user before refresh this page "

... but dont show this.

///////////////////////index.php//////////////////////////
 <?php
    session_start();
    if(isset($_POST['reg_code'])){
       if (!empty($_POST['reg_code'])) {
          $_SESSION ['reg_code'] =htmlspecialchars($_POST['reg_code']);
       }
    }

    echo $_SESSION ['reg_code'];
 ?>

<form action="index.php" method="POST"  enctype="multipart/form-data">
    <center>
       please enter your key pass :
       <br>
       <br>
       <input placeholder="type here..." type="text" style="width: 400px; border-right: 8px; text-align: center;" name="reg_code"><br><br>
       <input type="submit" name="submit" value="submit">
    </center>
</form>

You please try like this.

<?php
    session_start();
    if(isset($_POST['reg_code']) && !empty($_POST['reg_code'])){
        $_SESSION ['reg_code'] =htmlspecialchars($_POST['reg_code']); 
    }

    if(isset($_SESSION ['reg_code']) && !empty($_SESSION ['reg_code'])){
        echo $_SESSION ['reg_code'];
    }


 ?>
<form action="" method="POST"  enctype="multipart/form-data">
  <center>
please enter your key pass :
    <br>
    <br>
    <input placeholder="type here..." type="text" style="width: 400px; border-right: 8px; text-align: center;" name="reg_code"><br><br>
    <input type="submit" name="submit" value="submit">
 </center>
</form>

I hope this will helps you. Thanks.

I think you are getting $_SESSION ['reg_code'] == null. If session doesn't contain any value then it will not work. Just try to get the data $_POST['reg_code'] is empty or not.

tested. It'sworking

<?php
session_start();
if(isset($_POST['reg_code']) && !empty($_POST['reg_code'])){
$_SESSION ['reg_code'] =htmlspecialchars($_POST['reg_code']);
echo $_SESSION ['reg_code'];
}
?>
 <form action="" method="POST"  enctype="multipart/form-data">
<center>
    please enter your key pass :
    <br>
    <br>
    <input placeholder="type here..." type="text" name="reg_code"><br><br>
    <input type="submit" name="submit" value="submit">
</center>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM