user5344752
user5344752

Reputation:

Issues while creating a session

I am creating a user session but it's like code has gone wrong somewhere and doesn't hold session, I simply redirect user to a blank page where I echo user id of session holder to check whether it's working or not. Their is no problem with table or db as register page works fine and data gets stored in db. My login.php

<?php
$con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established");
?>

<?php
if(isset($_POST['login'])){ 
 $username = strip_tags(@$_POST['user_login']);
 $password = strip_tags(@$_POST['password_login']);    
 $result = $con->query("SELECT * FROM users WHERE username='$username' AND password='$password'");
 $row = $result->fetch_array(mysqli_both);
 session_start();
 $_SESSION["userid"] = $row['userid'];
 header("Location: test12.php");
 }
 ?>

    <h2>Already a member? Login Below</h2>
    <form action="" method="POST">
    <input type="text" name="user_login" size="25" placeholder="Username"/><br><br>
    <input type="password" name="password_login" size="30" placeholder="Passsword"/><br><br>
    <input type="submit" name="login" value="Login">
    </form>

test.php for checking session

<?php
$con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established");
?>
<?php  
session_start();
?>
<div style="margin-left:30px;margin-right:400px;border:1px solid #000;">
<h2>User id is:</h2>
<?php echo $_SESSION["userid"]; ?>
</div>

Upvotes: 1

Views: 51

Answers (2)

Xarrainho
Xarrainho

Reputation: 1

try by using $_SESSION['userid'] instead of $_SESSION["userid"].

Upvotes: 0

Awais Tahir
Awais Tahir

Reputation: 178

Make session_start(); your first line in the file

Upvotes: 1

Related Questions