title: How to prevent PHP SESSION closing when the page is refreshed?
body: <p>I have created Signup and Login systems for my gallery website. When a user tries to login into system, their user and password and admin privilege is checked. If it was successful, the username is appeared on top left corner of the home page and Login turns to Logout. The problem is that when I refresh the page, the user is logged out.</p>
  <p>login.php code:</p>
  <pre><code>&lt;?php
  
  session_start();
  
  class User
  
  {
  
      public function CheckUser()
  
      {
  
          require &quot;../app/core/database.php&quot;;
  
          if (isset($_POST['username']) &amp;&amp; isset($_POST['pass'])) {
  
              $username = $_POST['username'];
  
              $password = $_POST['pass'];
  
              //to prevent sql injection
  
              $username = stripcslashes($username);
  
              $password = stripcslashes($password);
  
              $username = mysqli_real_escape_string($connection, $username);
  
              $password = mysqli_real_escape_string($connection, $password);
  
              $sql = &quot;SELECT * FROM signup WHERE username = '$username' and password = '$password'&quot;;
  
              $sql2 = &quot;SELECT 'admin' FROM signup&quot;;
  
              $log_result = mysqli_query($connection, $sql);
  
              $count = mysqli_num_rows($log_result);
  
              if ($count == 1) {
  
                  $_SESSION['loggedin'] = true;
  
                  $_SESSION['username'] = $username;
  
                  $_SESSION['is_admin'] = mysqli_query($connection, $sql2);
  
                  header(&quot;Location: ../home/index&quot;);
  
              } else {
  
                  echo &quot;&lt;script&gt;Invalid()&lt;/script&gt;&quot;;
  
              }
  
          }
  
      }
  
  }
  
  ?&gt;
  
  &lt;script&gt;
  
      function Invalid() {
  
          alert(&quot;Invalid user/password&quot;);
  
      }
  
  &lt;/script&gt; 
  </code></pre>
  <p>a part of home page code (index.php):</p>
  <pre><code>&lt;?php
  
  error_reporting(E_ALL);
  
  ini_set('display_errors', TRUE);
  
  include &quot;../app/model/loadImages.php&quot;;
  
  include &quot;../app/core/config.php&quot;;
  
  include &quot;../app/model/login.php&quot;;
  
  ?&gt;
  
  &lt;body&gt;
    &lt;nav&gt;
      &lt;?php while ($row = $result2-&gt;fetch_assoc()) {
        $rows[] = $row ?&gt;
        &lt;div class=&quot;logo&quot;&gt;
          &lt;a href=&quot;index.php&quot;&gt;&lt;?php echo $row['header_1'] ?&gt; &lt;em&gt;&lt;?php echo $row['header_2'] ?&gt;&lt;/em&gt;&lt;/a&gt;
          &lt;span style=&quot;font-weight: normal; color:white;&quot;&gt;
            &lt;label&gt;
              &lt;?php
              if (isset($_SESSION['loggedin']) &amp;&amp; isset($_SESSION['username'])) {
                echo $_SESSION['username'];
              } ?&gt;
            &lt;/label&gt;
          &lt;/span&gt;
        &lt;/div&gt;
        &lt;div class=&quot;menu-icon&quot;&gt;
          &lt;span&gt;&lt;/span&gt;
        &lt;/div&gt;
        &lt;/nav&gt;
   &lt;section class=&quot;overlay-menu&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;main-menu&quot;&gt;
            &lt;ul&gt;
  
              &lt;li&gt;
                &lt;?php
                if (!isset($_SESSION['loggedin']) &amp;&amp; !isset($_SESSION['username'])) {
                  echo &quot;&lt;a href='/MyProject/public/login/index.php'&gt;LogIn&lt;/a&gt;&quot;;
                } else {
                  echo &quot;&lt;a href='' action='EndSession();'&gt;Logout&lt;/a&gt;&quot;;
                  echo &quot;&lt;li&gt;&quot;;
                  echo &quot;&lt;a href='/MyProject/public/admin/index'&gt;Admin Area&lt;/a&gt;&quot;;
                  echo &quot;&lt;/li&gt;&quot;;
                } ?&gt;
  
              &lt;/li&gt;
              &lt;li&gt;
                &lt;a href=&quot;about.html&quot;&gt;About Us&lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;a href=&quot;blog.html&quot;&gt;Blog Entries&lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;a href=&quot;single-post.html&quot;&gt;Single Post&lt;/a&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
            &lt;?php foreach ($rows as $row) { ?&gt;
              &lt;p&gt;&lt;?php echo $row['message_1'] ?&gt;&lt;/p&gt;
            &lt;?php } ?&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  &lt;script&gt;
      function EndSession() {
  
        &lt;?php session_unset(); ?&gt;
      }
    &lt;/script&gt;
  </code></pre>
  <p>index.php for login page:</p>
  <pre><code>&lt;?php
  include &quot;../app/core/config.php&quot;;
  include &quot;../app/model/login.php&quot;;
  
  $login = new User();
  $login-&gt;CheckUser();
  
  ?&gt;
  
  &lt;body&gt;
      
      &lt;div class=&quot;limiter&quot;&gt;
          &lt;div class=&quot;container-login100&quot; style=&quot;background-image: url('../../app/views/login/images/bg-01.jpg');&quot;&gt;
              &lt;div class=&quot;wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54&quot;&gt;
                  &lt;form class=&quot;login100-form validate-form&quot; method=&quot;POST&quot;&gt;
                      &lt;span class=&quot;login100-form-title p-b-49&quot;&gt;
                          Login
                      &lt;/span&gt;
  
                      &lt;div class=&quot;wrap-input100 validate-input m-b-23&quot; data-validate = &quot;Username is reauired&quot;&gt;
                          &lt;span class=&quot;label-input100&quot;&gt;Username&lt;/span&gt;
                          &lt;input class=&quot;input100&quot; type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;Type your username&quot;&gt;
                          &lt;span class=&quot;focus-input100&quot; data-symbol=&quot;&amp;#xf206;&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
  
                      &lt;div class=&quot;wrap-input100 validate-input&quot; data-validate=&quot;Password is required&quot;&gt;
                          &lt;span class=&quot;label-input100&quot;&gt;Password&lt;/span&gt;
                          &lt;input class=&quot;input100&quot; type=&quot;password&quot; name=&quot;pass&quot; placeholder=&quot;Type your password&quot;&gt;
                          &lt;span class=&quot;focus-input100&quot; data-symbol=&quot;&amp;#xf190;&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      
                      &lt;div class=&quot;text-right p-t-8 p-b-31&quot;&gt;
                          &lt;a href=&quot;#&quot;&gt;
                              Forgot password?
                          &lt;/a&gt;
                      &lt;/div&gt;
                      
                      &lt;div class=&quot;container-login100-form-btn&quot;&gt;
                          &lt;div class=&quot;wrap-login100-form-btn&quot;&gt;
                              &lt;div class=&quot;login100-form-bgbtn&quot;&gt;&lt;/div&gt;
                              &lt;button class=&quot;login100-form-btn&quot;&gt;
                                  Login
                              &lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class=&quot;txt1 text-center p-t-54 p-b-20&quot;&gt;
                          &lt;span&gt;
                              Or Sign Up Using
                          &lt;/span&gt;
                      &lt;/div&gt;
  
                      &lt;div class=&quot;flex-c-m&quot;&gt;
                          &lt;a href=&quot;#&quot; class=&quot;login100-social-item bg3&quot;&gt;
                              &lt;i class=&quot;fa fa-google&quot;&gt;&lt;/i&gt;
                          &lt;/a&gt;
                      &lt;/div&gt;
  
                      &lt;div class=&quot;flex-col-c p-t-155&quot;&gt;
                          &lt;span class=&quot;txt1 p-b-17&quot;&gt;
                              Or Sign Up Using
                          &lt;/span&gt;
  
                          &lt;a href=&quot;&lt;?php echo $root ?&gt;/public/signup/index.php&quot; class=&quot;txt2&quot;&gt;
                              Sign Up
                          &lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  <p>How can I fix this problem?</p>