Jump to content

How to SESSION get information from the database


hain

Recommended Posts

I have these two files and I need to solve the problem that I need the user data in the role column to be taken when logging in. I have written it so that the data can be extracted from the database, but it does not work for me that it is “forwarded” from process.php to admin.php . Please don’t know what the error is that the admin.php file doesn’t want to load $ _SESSION['Role'] from process.php ? Thanks to everyone for helping and here is the code:

process.php

<?php 
require_once('connect.php');
session_start();
    if(isset($_POST['Login']))
    {
       if(empty($_POST['Username']) || empty($_POST['Password']))
       {
            header("location:index.php?Empty= Please Fill in the Blanks");
       }
       else
       {
            $query="select * from role_test where Username='".$_POST['Username']."' and Password='".md5($_POST['Password'])."'";
            $result=mysqli_query($con,$query);

            if(mysqli_fetch_assoc($result))
            {
                $_SESSION['User']=$_POST['Username'];

                while($row = mysqli_fetch_array($result) ){
                $_SESSION['Role']=$row['role'];
                }

                header("location:admin.php");
            }
            else
            {
                header("location:index.php?Invalid= Please Enter Correct User Name and Password ");
            }
       }
    }
    else
    {
        echo 'Not Working Now Guys';
    }

?>

 

admin.php

<?php
    session_start();

    if(!(isset($_SESSION['User'])))
    {
        header("Location: index.php");
        exit(0);
    }	  

?>
<!DOCTYPE html>
<html>
<head>
<title>Role</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
$_SESSION['Role']=$role;

echo $role;

?>

</body>
</html>

 

 

 

Link to comment
Share on other sites

This is exactly what I need to get the role of the user who logs in to the process.php
which should be this: while ($ row = mysqli_fetch_array ($ result)) {
                 $ _SESSION ['Role'] = $ row ['role'];
                 }
and then to set it to $ role in admin.php and list it in the echo for the user

Link to comment
Share on other sites

Not specifically based on what you have posted. At this point we can only tell you that the session variable is not set at the point you are trying to use it. If you don't want tell us the error you are getting or any details then there is little else we can do.

Link to comment
Share on other sites

23 minutes ago, gw1500se said:

Not specifically based on what you have posted. At this point we can only tell you that the session variable is not set at the point you are trying to use it. If you don't want tell us the error you are getting or any details then there is little else we can do.

good thank you, but the problem is that the result in the echo is none (nothing is displayed), it will be because the variable is not set as you write above, so please advise why the variable is not transferred from prosess.php to admin.php

Link to comment
Share on other sites

Did you move the session start to before the include as I originally suggested? The next step is to include  a print of $_SESSION at the end of the script that sets that variable to make sure it is set at the end of that script and again after the session start of the script that uses it. That will determine if the variable is being reset somewhere in the first script or in the 2nd before it is used. This assumes there are no intervening scripts you didn't mention that can mess it up.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.