title: How do I access session variable from php in external javascript
body: <p>I know this has been asked before but there's no explanation from start to finish. I have a <code>login.php</code> from which I want to get <code>$_SESSION['user_id']</code> </p>
  
  <p>from</p>
  
  <pre><code>&lt;?php
  include "connect.php";
  
  session_start();
  
  $email=$_POST['email'];
  $password=$_POST['password'];
  
  $select_data=mysql_query("select * from user where email='$email' and password='$password'");
  
  if(isset($_POST['email']))
  {
  if($row=mysql_fetch_array($select_data))
   {
    $_SESSION['user_id']=$row['id']; //The variable want to access from ext.js
  
    echo "success";
   }
   else
   {
    echo "fail";
   }
   exit();
  }
  ?&gt;
  </code></pre>
  
  <p>By the way, this authenication works fine</p>
  
  <p>My Main.php</p>
  
  <pre><code>
  &lt;!DOCTYPE html&gt;
  &lt;?php include 'connect.php';
  session_start();
  echo $_SESSION['user_id']; //works fine
  //;
  ?&gt;
  &lt;body&gt;
  //contents
  &lt;script src="login.js"&gt;&lt;/script&gt;
  &lt;script src="postad.js"&gt;&lt;/script&gt; //the script from which I want to get $SESSION_['user_id']
  
  &lt;/body&gt;
  </code></pre>
  
  <p>I want this variable to insert later on in a database. Any suggestions?</p>