-2

I am trying to set the user's info in session variable because I am gonna use it in another page but I am getting issue related to header.

When can we destroy the session the error:

Warning: session_start(): Session cannot be started after headers have already been sent

Here is my registration.php code

<?php
session_start();
?>

<?php


if (isset($_POST["register-submit"])) {
    global $wpdb;
    $username = esc_sql($_POST["username"]);
    $shopname = esc_sql($_POST["shopname"]);

    $gstnumber = esc_sql($_POST["gstnumber"]);
    $phonenumber = esc_sql($_POST["phonenumber"]);
    $password = esc_sql($_POST["password"]);
    $conf_password = esc_sql($_POST["conf_password"]);
    $api_key = "{api_key}";
    $endpoint_url = 'http://sheet.gstincheck.co.in/check/' . $api_key . '/' . $gstnumber;

    $_SESSION['user_data'] = array(
        'username' => $username,
        'shopname' => $shopname,
        'gstnumber' => $gstnumber,
        'phonenumber' => $phonenumber,
        'password' => $password
    );
    if ($password == $conf_password) {
        $response = file_get_contents($endpoint_url);
        $data = json_decode($response, true);


        if (!username_exists($username)) {
            if ($data['flag'] == true) {

            } else {
                echo '<script>alert("GST NO is invalid")</script>';

            }

        } else {
            echo '<script>alert("Username alredy Exists")</script>';
        }
    } else {
        echo '<script>alert("pass do not matched")</script>';
    }



}


?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registration Form</title>

</head>

<body>
    <div class="registration-container">
        <h2>Registration</h2>
        <form action="" method="POST">
            <--My Form Input-->
 </form>
        <p>Already have an account? <a href="your-login-page">Login here</a></p>
    </div>
</body>

</html>
user3783243
  • 5,368
  • 5
  • 22
  • 41
krish
  • 43
  • 4
  • What is the first question? – user3783243 Aug 08 '23 at 19:35
  • Likley get rid of `esc_sql` and use parameterized queries. `$_SESSION['user_data']` should only be set after verifying login. You shouldn't store passwords in plain text, and you should not store passwords in session data. Please consult tutorial as this seems like first time issues. – user3783243 Aug 08 '23 at 19:36
  • Nothing in provided code is destroying sessions – user3783243 Aug 08 '23 at 19:37

0 Answers0