-1

I have set the session which is working but If I refresh the page then I am getting error

Undefined index: email_error

and if I clicked on submit button then it is working. I also set the empty but not working.

There is some issue in session.

php

<?php 
session_start();

if (isset($_POST['submit'])) {

    $_SESSION['email']=$_POST['email'];
    $_SESSION['email_error']="";

            if (empty($_SESSION['email'])) {
                    $_SESSION['email_error']="email is empty";  
                    header('location:index.php');
            }

    }

 ?>

HTML

<?php
session_start();

$email_error="";
$email_error=$_SESSION['email_error'];
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<form action="process.php" method="post">
     <span class="error"><?php echo $email_error;?></span>
    <input type="email" name="email">
    <input type="submit" name="submit">
</form>

</body>
</html>
<?php
session_destroy();
 ?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Naren Verma
  • 2,205
  • 5
  • 38
  • 95
  • The solution for this is removing the session_destroy(); line. This will clear your session. – Sander Jun 02 '17 at 09:57
  • Sorry Mr.Sander, I deleted my session_destory() from HTML file but still getting error – Naren Verma Jun 02 '17 at 09:59
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – CBroe Jun 02 '17 at 09:59
  • No major here.you are not setting any values to that.do like $_SESSION['email_error']=''; – lalithkumar Jun 02 '17 at 10:00
  • Check if your $_SESSION['email_error'] is being set. – Sander Jun 02 '17 at 10:01
  • Yes Mr.lalithkumar, I haven't set any value for that. If I clicked in submit button then it will assign the value. – Naren Verma Jun 02 '17 at 10:02

3 Answers3

3

Do like:

<?php
session_start();

$email_error="";
$email_error=isset($_SESSION['email_error'])?$_SESSION['email_error']:'';
?>
lalithkumar
  • 3,480
  • 4
  • 24
  • 40
1

The session_destroy() deletes your current session, so each refresh is a new session.

session_destroy is for deleting a session

for more info: php docs

katona.abel
  • 771
  • 4
  • 12
  • I know session_destory() will delete the session but without creating my session it's getting error – Naren Verma Jun 02 '17 at 09:57
  • you are wrong for logout you don't use `session_destroy()` function, you can use unset() for logout. like unset($_SESSION['loginid]); – Archish Jun 02 '17 at 09:57
  • you have to create a session to be able to save data to it, but you do not have to destroy it. destroy means you delete it, with its contents – katona.abel Jun 02 '17 at 09:58
  • I haven't created session till now. If I clicked on submit then it will create the session. right? – Naren Verma Jun 02 '17 at 10:01
-1

remove session_destroy(); from last then it will work.

zarif khan
  • 81
  • 6