hi, i am very new in php programming. Pls help me how to store session variable into my sql database. Below is my code

<?php
    function userIsLoggedIn()
        {
            if (isset($_POST['action']) and $_POST['action'] == 'login')
                {
                    if (!isset($_POST['loginid']) or $_POST['loginid'] == '' or !isset($_POST['password']) or $_POST['password'] == '')
                        {
                            $GLOBALS['loginError'] = 'Please fill in both fields';
                            return FALSE;
                        }

                    $password = $_POST['password'];

                    if (dbuserright($_POST['loginid'], $password))

                        {
                            session_start();
                            $_SESSION['loggedIn'] = TRUE;
                            $_SESSION['loginid'] = $_POST['loginid'];
                            $_SESSION['password'] = $password;
                            return TRUE;
                        }
                    else
                        {
                            session_start();
                            unset($_SESSION['loggedIn']);
                            unset($_SESSION['loginid']);
                            unset($_SESSION['password']);
                            $GLOBALS['loginError'] ='The specified loginid  or password was incorrect.';
                            return FALSE;
                        }
                }
            if (isset($_POST['action']) and $_POST['action'] == 'logout')
                {
                    session_start();
                    unset($_SESSION['loggedIn']);
                    unset($_SESSION['loginid']);
                    unset($_SESSION['password']);
                    header('Location: ' . $_POST['goto']);
                    exit();
                }

            if (isset($_SESSION['loggedIn']))
                {
                    return dbuserright($_SESSION['loginid'],
                    $_SESSION['password']);
                }
        }


    function dbuserright($loginid, $password)
        {
            include '../config/dbconnection.php';
            try
                {
                    $sql = 'SELECT COUNT(*) FROM userright  WHERE loginid = :loginid AND password = :password';
                    $s = $pdo->prepare($sql);
                    $s->bindValue(':loginid', $loginid);
                    $s->bindValue(':password', $password);
                    $s->execute();
                }
            catch (PDOException $e)
                {
                    $output = 'Error searching for user.';
                    include '../inc/errormsg.inc.php';
                    exit();
                }
            $row = $s->fetch();

            if ($row[0] > 0)
                {
                    return TRUE;
                    $GLOBALS['loginError'] ='The specified was correct.';
                }
            else
                {
                    return FALSE;
                }
        }

    function userHasRole($role)
        {
            include '../config/dbconnection.php';
            try
                {
                    $sql = "SELECT COUNT(*) FROM userright INNER JOIN userrole ON userright.id = userid INNER JOIN role ON roleid = role.id
                    WHERE loginid = :loginid AND role.id = :roleId";
                    $s = $pdo->prepare($sql);
                    $s->bindValue(':loginid', $_SESSION['loginid']);
                    $s->bindValue(':roleId', $role);
                    $s->execute();
                }
            catch (PDOException $e)
                {
                    $output = 'Error searching for author roles.';
                    include '../inc/errormsg.inc.php';
                    exit();
                }
            $row = $s->fetch();

            if ($row[0] > 0)
                {
                    return TRUE;
                }
            else
                {
                    return FALSE;
                }
        }
?>
Member Avatar for LastMitch

hi, i am very new in php programming. Pls help me how to store session variable into my sql database. Below is my code

I tend like to ask this question often before I starting helping any members.

Did you write this code?

I mean the code is pretty advance.

You are using PDO connection.

Was there an error when you ran the code?

I think you have too many sessions.

If you wrote this code don't you feel you have too many sessions?

Hi

There is no error in running code. initially I got lot of error. One by one , i have cleared.

I have referred the book php "PHP & MYSQL" by kavin and based on his instruction i have wrote for my environment. I have just started my PHP just 3 week ago. First i tried to store the session in database
It is not in that book. since I have started just 3 weeks, still I am hanging in login form only.
and also i do not know how to retrive the session variable..... that why i am here..

tq

maideen

Member Avatar for LastMitch

I have just started my PHP just 3 week ago.

You started PHP 3 weeks ago?

It is not in that book. since I have started just 3 weeks, still I am hanging in login form only.
and also i do not know how to retrive the session variable.....
I think this code is too hard for you. There's more work involved.

You need to add alot of things in your code.

You need to create a function for a UPDATE statement and an INSERT statement too.

First i tried to store the session in database

Can you post your table?

Do you have a timestamp or a session_id in your table?

If you don't have timestamp or a session_id then you need to add a column called timestamp and session_id in your table.

that why i am here..

Personally, I feel you need to learn basic PHP then move to something like this.

The code seems to be messy & involves lots of unnecessary checkings.
First try in simple way.
1. Declare a session variable /Or get an automatic session variable.
2. Set and pass the variable.
3. Insert the variable into database.

thank you LastMitch and Jessfly. Yes I have to go basic first. once i completed the insert,update,select, then i will post my code for further session storage in database

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.