title: Php session variables for user authentication on multiple pages
body: <p>I am creating a content management website with a login page at the back end. </p>
  
  <p>I have created a working login page for the main menu of the content management system. </p>
  
  <p>The below images show my code.</p>
  
  <p><strong>1st Step - User visits the URL of main menu for content management system:
  Code shown below</strong>. </p>
  
  <pre><code>&lt;?php
  session_start();
  
  echo $_SESSION['valid_user'];
  
  if(!isset($_SESSION['valid_user']))
  {
  $URL="error.php";
  header("Location: $URL");
  }
    ?&gt;
  </code></pre>
  
  <p><strong>Two things can happen. 1 if they have not logged in before they will be directed to an error page.</strong>
  They can then select to visit the login page and login using their user name and pswd. </p>
  
  <p>i have declared ?php start_session();? at the top</p>
  
  <pre><code>&lt;?php
  
  $login = $_POST['name'];
  $loginpass =$_POST['password'];
  
   if((isset($login)) || (isset($loginpass))){
  
      //echo "&lt;p&gt; form has been submitted&lt;/p&gt;";
  
      include("connect.php");
  
      $query = "select * from logins where 
          username='$login' and pswd=MD5('$loginpass')";
        $result = mysql_query($query) or die($query."&lt;br/&gt;&lt;br/&gt;".mysql_error());
  
      $count=mysql_num_rows($result);
      echo $count;
  
  if($count &gt; 0){
  
     echo "&lt;p&gt;you are logged in as $login please 
     go to &lt;a href='home2.php'&gt;edt     home&lt;/a&gt;.&lt;/p&gt;";
      echo "&lt;p&gt;&lt;a href='logout.php'&gt;log out&lt;/a&gt; $login?&lt;/p&gt;";
      $_SESSION['valid_user'] = $login;
  
  }else{
  
      echo "&lt;p&gt;sorry login failed&lt;/p&gt;";
  
  }
  
    }else{
  
      //echo "&lt;p&gt; form  hasn't been submitted&lt;/p&gt;";
      //Visitor needs to enter a name and password                
      echo "&lt;h1&gt;Please Log In&lt;/h1&gt; &lt;form method='post' action='index2.php'&gt;";
      echo "&lt;p&gt;Username: &lt;input type='text'name='name'&gt;&lt;/p&gt;";
      echo "&lt;p&gt;Password: &lt;input type='password' name='password'&gt;&lt;/p&gt;";
      echo "&lt;p&gt;&lt;input type='submit' name='submit' value='Log In'&gt;&lt;/p&gt;&lt;/form&gt;";        
   }
  
  
   ?&gt;
  </code></pre>
  
  <p>So far all of that functionality works fine for me. </p>
  
  <p>However, I want to make sure that if the user bypasses the home url and decides to jump straight to a section within the edit menu, they will be either forced to login, if they have not already, or, the php will check their credentials if they have logged in. </p>
  
  <p>This is an example of the code I have at the top of the page I want to place my validation ont. I'm not sure if already have a database table connection at the top will affect the session variable. </p>
  
  <pre><code> &lt;?php
  
  include("connect.php");
  //echo "all good here";
  
  
  //grab the data from the table 'designs'
  $query ="SELECT * FROM designs ORDER BY id";
  
  
  //send SQL statement to MySQL server
  $display = mysql_query($query);
  $num = mysql_numrows($display);
  
  
   mysql_close();
   ?&gt;
  </code></pre>
  
  <p>I know i want to place  php scripts on all of the php pages that check the 'vaild_user' session variable is set and also give the user the ability to logout by pointing to the logout.php file. Im just not sure how to go about doing it at this point. </p>
  
  <p>I am very new to all this and generally understand following a clear guide, like most people I'm sure. </p>
  
  <p>Any help anyone could give would be greatly appreciated</p>
  
  <p>Thanks again!. </p>