1 Replies - 162 Views - Last Post: 31 October 2013 - 05:26 PM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 156
  • Joined: 07-October 12

Setting a Session Variable for Login

Posted 31 October 2013 - 04:38 PM

Right now I'm trying to get a script that will create a session variable to make a user logged in when the criteria matches. So far this is what I have to set it all up.
<div class="header">
<div class="login-push"></div>
<div class="login">
<?php 
session_start();
if($_SESSION['username'] == '')
{

echo '<form action="functions/login.php" method="post">
<input type="text" name="email" id="email">
<input type="password" name="password" id="password">
<input type="submit" id="submit" value="Login" class="Login-Button" >';
}
else {
echo "Hello " .$_SESSION['username'];
}
?>
</form>
</div>
</div>


That's my header.php within my includes folder.
And then for my login script I have
<?php
include '../config.php';

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM user WHERE email='$email' AND password='$password'";
echo $query;
$result = mysqli_query($con,$query);
$num_rows=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
if($num_rows==1)
{
$_SESSION['username']=$row['username'];
echo "Logged In"; echo $_SESSION['username'];
echo $row['username'];
}
else {
echo "Wrong";
}
?>


Now when the user goes back to the main page and refreshes the Session Variable is unset and it shows the login in the header. Any idea why the Session variable isn't persistent?

Is This A Good Question/Topic? 0
  • +

Replies To: Setting a Session Variable for Login

#2 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 156
  • Joined: 07-October 12

Re: Setting a Session Variable for Login

Posted 31 October 2013 - 05:26 PM

Let's go ahead and ignore this question. If anyone else has this issue. My login.php did not have
session_start()


After added it's working fine. Sorry for the question that was so simple.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1