Warning: session_destroy(): Trying to destroy uninitialized session[session_start() used] : <p>i know my code is very beginner because i am just started php.
so sorry at beginning</p>

<p>I already read stack other questions and answers about</p>

<p><strong>Warning: session_destroy(): Trying to destroy uninitialized session</strong></p>

<p>all answers just mentioned that i need to use <strong><em>session_start()</em></strong> before using </p>

<p><strong><em>session_destroy();</em></strong></p>

<p>but they are not explaining why this warning occurs even i already used <strong><em>session_start()</em></strong>
i am just trying to login then dashboard page appears where user can click on logout button to logout</p>

<p>whole application is working fine only problem is when i click on logout button in dashboard.php it go to logout page and show warning which above mentioned</p>

<p>login.php</p>

<pre><code>&lt;?php
session_start();
}
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Login Page&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div class="header"&gt;
  &lt;h1 align="center"&gt;Login&lt;/h1&gt;
&lt;/div&gt;
  orm action="" method="post"&gt;
  &lt;table align="center"&gt;
    &lt;tr&gt;
&lt;td align="right"&gt;&lt;input type="email" name="email" placeholder="Email" required&gt;&lt;/td&gt;
&lt;td class="s1"&gt;*&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
&lt;td align="right"&gt;&lt;input  type="password" name="pass" placeholder="Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required&gt;&lt;/td&gt;
&lt;td class="s1"&gt;*&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
&lt;td align="right"&gt;&lt;input type="submit" value="Login"&gt;&lt;td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;p align="center"&gt;&lt;strong&gt;Or&lt;/strong&gt;&lt;/p&gt;
  &lt;p align="center"&gt;Create New acount &lt;strong&gt;&lt;input type="button" value="Sign Up" onclick="location.href='sign_up.php'"&gt;&lt;/strong&gt;&lt;/p&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
&lt;div class="footer"&gt;
  &lt;p&gt;&lt;span class="s1"&gt;*&lt;/span&gt; indicates mandatory feild&lt;p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;?php
require 'connection.php'; // connection

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$email = $_POST['email'];
$pass = $_POST['pass'];
// getting username from database and storing into session variable
///  $sql =mysqli_query($conn,"SELECT name FROM users WHERE email='$email'");
  $sql1 = mysqli_fetch_assoc($sql);
  $user_name = $sql1['name'];
//  setting session variables
  $_SESSION['user_name']= $cookie_name;
  $_SESSION['timeout']=time();

// check email exists or not
$query=mysqli_query($conn,"SELECT email FROM users WHERE email='$email'");
if(mysqli_num_rows($query) &gt; 0)
{
  //check email and password correct or not
  $query1=mysqli_query($conn,"SELECT email, password FROM users WHERE email='$email' AND password='$pass'");
  if(mysqli_num_rows($query1) &gt; 0)
  {
    // login success
    header("location:dashboard.php");
    // ----&gt;  alternative way to redirect to dashboard.php
  /*echo  "&lt;script type='text/javascript'&gt;
window.location.href = 'dashboard.php'
&lt;/script&gt;";*/
  }
  else
  {
    // incorrect password
    echo "&lt;p align='center' style='color:#ff6262'&gt;your password is incorrect!&lt;br&gt;Please try again&lt;/p&gt;";
  }
}
else
{
  // email not exist
    echo "&lt;p align='center' style='color:#ff6262'&gt;&lt;b&gt;Email you are entering does not exist in our database&lt;br&gt;&lt;/b&gt;&lt;/p&gt;";
}
 }
 ?&gt;
</code></pre>

<p>dashboard.php</p>

<pre><code>&lt;?php
session_start();
?&gt;

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;
    &lt;?php
require 'connection.php';
echo $_SESSION['user_name'];
    ?&gt;
  &lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div class="header"&gt;  &lt;h1 align="center"&gt;&lt;?php echo "Welcome:  ".ucfirst($_SESSION['user_name']); ?&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;table&gt;
  &lt;tr&gt;
&lt;td align="right"&gt;Change User Name:&lt;/td&gt;&lt;td&gt;&lt;input type="button" onclick="location.href='user.php'" value="click here"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="right"&gt;Change Password:&lt;/td&gt;&lt;td&gt;&lt;input type="button" onclick="location.href='pass.php'" value="click here"&gt;&lt;/td&gt;
&lt;/tr colspan="2"&gt;
&lt;tr&gt;&lt;td align="right"&gt;&lt;input type="button" value="Logout" onclick="location.href='logout.php'"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt;
&lt;?php
if (isset($_SESSION['timeout']) &amp;&amp; (time() - $_SESSION['timeout'] &gt; 60)) {

    session_unset();     // unset $_SESSION variable for the run-time
    session_destroy();   // destroy session data in storage
    header("location:logout.php");
}
$_SESSION['timeout'] = time();

 ?&gt;
</code></pre>

<p>logout.php</p>

<pre><code>&lt;?php
session_start();
?&gt;
&lt;?php
session_unset();
if(!session_unset())
{
  echo "session not unset";
}
else {
  echo "session unset";
}

session_destroy();
if(!session_destroy())
{
  echo "session not destroyed";
}
else {
  echo "session destroyed";
}
//header(location:login.php);
 ?&gt;
</code></pre>

<p>some other suggest me to use cookies but i don't know how to relate cookies with session anyone who can help.</p>
