title: Creating and storing session in PHP
body: <p>I have login.php folder and addpost.php folder. I want to create session and store session in login.php and if it is created it will redirect to addpost.php. In addpost.php i want to check if there's no session stored it will redirect to login.php again.</p>
  
  <p>My Problem: it works fine when i don't put my code in addpost.php yet but when i put my code there and come back to test my code i can't redirect login page to addpost page.</p>
  
  <p>here's my code in folder login.php</p>
  
  <pre><code>&lt;?php
  require('../koneksi.php');
  if(isset($_POST['login'])){
  
      $username = mysqli_real_escape_string($kon, ($_POST['username']));
      $password = mysqli_real_escape_string($kon, ($_POST['password']));
      $query = "SELECT * FROM admin WHERE username = '$username' AND password = '$password'";
      $result = mysqli_query($kon, $query);
      if(!$result || mysqli_num_rows($result) == 0){
          echo '&lt;script&gt;alert("Username atau password Anda salah!")&lt;/script&gt;';
      } else {
          $_SESSION['admin'] = 'username';
          $_SESSION['admin'] = $username;
  
          header('Location: addpost.php');
      }
  }
  </code></pre>
  
  <p>?></p>
  
  <p>here's my code in addpost.php</p>
  
  <pre><code>&lt;?php
  require('../koneksi.php');
  session_start();
  if(!isset($_SESSION['admin'])){
      header('Location: index.php');
  } ?&gt;
  </code></pre>
  
  <p>could you guys help me with this! thank you!</p>