Jump to content

Session Function not quite working properly [SOLVED]


scottybwoy

Recommended Posts

Hi,  I have a login function that goes off anf finds the relative details for logging in and then procedes to open up the session to insert the data.  Let me show you what I have :
[code]
<?php

function login()
{
$this->authorise($this->getUserName());

if ($this->authorise->success = TRUE)
{
global $username, $user_id, $data;

$this->sessCon();

session_start();

$data = $_SESSION["SESSION_USER_ID"];
$qry = mssql_query("SELECT * FROM sessTbl WHERE value LIKE '%" . $data . "%'")
or die("Session ID Query Failed");

$username = $this->getUserName();
$user_id = $this->getUserId($username);

if (!$row = mssql_fetch_row($qry))
{
$_SESSION["SESSION_USER_ID"] = $user_id;
$_SESSION["SESSION_USERNAME"] = $username;
}

} else {
die ("Get Out!");
}

}
?>
[/code]

I tried using an If (!isset($_SESSION["SESSION_USER_ID"]))  But that didn't work it just tried writing the new session data over the top of the existing data, but as am using database storage for it I just got an error of trying to put data in twice.

But now all it does is wright the username and ID in, but on refresh it will just make a new session key and not put any data in at all, so it's a bit closer but not what I want.

Can anybody help me out on what I can do to make it work, thanks
Link to comment
Share on other sites

Realised that was never going to work as the old data would always be (until carbage collection) there so I changed it back to this :
[code]
<?php
function login()
{
$this->authorise($this->getUserName());

if ($this->authorise->success = TRUE)
{
global $username, $user_id;

session_start();

if (!isset($_SESSION["SESSION_USER_ID"]))
{
$_SESSION["SESSION_USER_ID"] = $user_id;
$_SESSION["SESSION_USERNAME"] = $username;
}

} else {
die ("Get Out!");
}

}
?>
[/code]
But now it still keeps making a new session ID, surely this is a standard procedure script, that many of you already have a good working way round, so please could you share a moment to help me please.  This is the second time i've posted on this issue and find it hard getting my head around it :(
Link to comment
Share on other sites

If I understand you correctly, you are saying that the values you assign to session variables are not persistant. You need to register these session variables first, as in the example below. You also need to make sure that you start the session on each page.

[code]
<?php
function login()
{
$this->authorise($this->getUserName());

if ($this->authorise->success = TRUE)
{
global $username, $user_id;

session_start();

if (!isset($_SESSION["SESSION_USER_ID"]))
{
session_register('SESSION_USER_ID');
$_SESSION['SESSION_USER_ID'] = $user_id;
session_register('USERNAME');
$_SESSION['SESSION_USERNAME'] = $username;
}

} else {
die ("Get Out!");
}

}
?>
[/code]
Link to comment
Share on other sites

I have it solved now, It was how I was calling the function, I changed it to this :
[code]
<?php

        global $username;

        sessCon();

$username = $this->getUserName();
$sql = mssql_query("SELECT * FROM sessTbl WHERE value LIKE '%" . $username . "%'");

        if (!$row = mssql_fetch_row($sql))
        {
        $this->login();
        }
?>
[/code]

Now it works!!
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.