-1

I want to get the session ID from the current user login , but there's something missing in my code, what is it, please help.

i have already added a Resort, and all i want to do is I want to list the resort what user created.

function resorts_list()
    {
        $db = ligoanandb();
        $ownerid = session_id();


        $sql = " SELECT * FROM resort WHERE ownerid = $ownerid order by 1 
        desc";


        $st = $db->prepare($sql);
        $st->bindParam(':ownerid',$ownerid, PDO::PARAM_INT);
        $st->execute();
        $rows = $st->fetchAll();
        $db = null;

        return $rows;
}

there's no result the resort does not list the current user's resort.

John Joe
  • 1
  • 1
  • 1
    Not sure if the session id is set or the correct value to use, but you aren't using the bind parameter correctly, in you SQL you need `ownerid = :ownerid` (`:` instead of `$`) – Nigel Ren Jan 13 '19 at 07:28
  • Possible duplicate of [how to get session variables using session id](https://stackoverflow.com/questions/8726268/how-to-get-session-variables-using-session-id) – Joseph_J Jan 13 '19 at 07:28
  • does session_id() = to session current user's id ? – John Joe Jan 13 '19 at 07:51
  • I would have thought that session id is not the ownerid. Usually that would be a session variable and stored when the user logs in. – Nigel Ren Jan 13 '19 at 07:53
  • what's the other way to get the current owner/user's id ? so i can specify the resort list. please any idea? – John Joe Jan 13 '19 at 07:55
  • No efforts made for finding the solution. Possible duplicate https://stackoverflow.com/questions/21302733/how-can-i-get-session-id-in-php-and-show-it – Umar Abdullah Jan 14 '19 at 05:34

2 Answers2

1

Did you put session_start() on the beggining of your script ?

Try this example, I have created table with fields id and ownerid and it works fine. Let me know if it works for you.

session_start();

function resorts_list()
{
    $db = new PDO("mysql:host=localhost;dbname=stack_test", "root", "");
    $ownerid = session_id();

    $sql = " SELECT * FROM table_new WHERE ownerid = :ownerid order by 1 
    desc";


    $st = $db->prepare($sql);
    $st->bindParam(':ownerid',$ownerid, PDO::PARAM_INT);
    $st->execute();
    $rows = $st->fetchAll();
    $db = null;

    return $rows;
}

print_r(resorts_list());
  • yes i did, from other file , and i just included it – John Joe Jan 13 '19 at 07:33
  • does session_id() = to session current user's id ? – John Joe Jan 13 '19 at 07:51
  • I have edited my post and you have code sample now. You can try it. session_id is id of current user session, and if you store this id into database you can identify user by this session id, but if user close browser and come again, session id will be generated again, and it will be different, so you must store it again. – Branislav M. Jan 13 '19 at 11:56
  • is there any other way , to get the id of the current user login? without using the session_id? – John Joe Jan 13 '19 at 14:17
  • sorry for the title , all i want is i want to list the resort where the current user created it. and the other user login they will not able to see it. like individual accounts for every users – John Joe Jan 13 '19 at 14:20
  • You must create two different tables in your db, one for users and one for resorts, then you need to join this 2 tables by user id. For example users table with columns user_id and email for example and resorts table with columns resort_id, resort_name, user_id. Then execute query `SELECT * FROM users INNER JOIN resorts WHERE users.user_id = resorts.user_id` – Branislav M. Jan 13 '19 at 15:38
  • thankyou for the idea i already got it function resorts_list() { $db = ligoanandb(); $owner = $_SESSION['username']; $sql = "SELECT * FROM resort WHERE ownerid = (select ownerid from users WHERE username = :owner) order by 1 desc"; $st = $db->prepare($sql); $st->bindParam(':owner',$owner); $st->execute(); $rows = $st->fetchAll(); $db = null; return $rows; } – John Joe Jan 13 '19 at 15:58
0

Add session_start() if you don't have it already. Otherwise you should put the user id to the session after the login.

Yasii
  • 1,702
  • 1
  • 10
  • 15
  • yes i already did, does session_id() = to session current user's id ? – John Joe Jan 13 '19 at 07:52
  • no you have to put the user id into the session as 'userid' or something – Yasii Jan 13 '19 at 08:48
  • how to automatically get the users id ? without using POST – John Joe Jan 13 '19 at 09:22
  • in your login page after you have authenticated, get the authenticated user's id and put it into the session like `$_SESSION["user_id"] = $id;` where $id is the id of the authenticated user; – Yasii Jan 13 '19 at 09:31