Jump to content

getting another session variable


mraza

Recommended Posts

hi all , i am working on a script which is oop driven and i m not much familiar with it, i appericiate if someone can help me to solve this problem , so basicaly current script is only setting one session variable to true if user login $_SESSION['is_successful_login']  , here is my code

<?php
include('files/db.php');
class ajaxLoginModule  {
  private $timeout         = null;
  private $target_element  = null;
  private $wait_text       = null;
  private $form_element    = null;
  private $wait_element    = null;
  private $notify_element  = null;
   
  function __construct() {
     include ('config.php'); 
 $msql  = new Db;
 $msql->connect();
 $this->is_login();
  } 
  function get_config() {
 $this->set_ajax_config();
  } 
  function set_ajax_config() {
     $this->timeout         = AJAX_TIMEOUT;
     $this->target_element  = AJAX_TARGET_ELEMENT;
     $this->wait_text       = AJAX_WAIT_TEXT;
 $this->wait_element    = AJAX_WAIT_ELEMENT;
     $this->notify_element  = AJAX_NOTIFY_ELEMENT;
     $this->form_element    = AJAX_FORM_ELEMENT;
  }
  function initLogin($arg = array()) {
 $this->get_config();
 $this->login_script();   	 
  }
  function initJquery() { 
 return "<script type='text/javascript' src='files/jquery-1.3.2.min.js'></script>";
  }
  function login_script() { 
 include ('files/login_script.php');
  }
  function is_login() {
      if(isset($_POST['username']))  {
     $username   = $_POST['username'];
	 $password   = $_POST['password'];
	 $strSQL = "SELECT * FROM ".USERS_TABLE_NAME."
			    WHERE username ='$username' AND password = '$password'
			   ";
        $result  = mysql_query ($strSQL); 
	$row     = mysql_fetch_row($result);
   /*
//THIS IS WHAT I NEED
$_SESSION['user'] = $row['username'];
$_SESSION['id'] = $row['id'];
*/
	$exist   = count($row);
	if($exist >=2) { $this->jscript_location();  } 
	else { $this->notify_show();}
	exit;		
  }   
  }
  function notify_show() {
    echo "<script>$('.".AJAX_NOTIFY_ELEMENT."').fadeIn();</script>";
  }
  function jscript_location() {
    $this->set_session();
    echo "<script> $('#container').fadeOut();window.location.href='".SUCCESS_LOGIN_GOTO."'</script>";
  }
  function set_session() {
      session_start();
  $_SESSION['is_successful_login'] = true;

  }  	 
  
}  
?>  

 

i comment that line what i need is username and id to store in those session variables

 

$_SESSION['user'] = $row['username'];
$_SESSION['id'] = $row['id']

 

i tried to add code in  function set_session but did not helped, appreciate  for any help.

 

Thanks

Link to comment
Share on other sites

thanks sir, yes i did like this

function is_login() {
      if(isset($_POST['username']))  {
       session_start();
     $username   = $_POST['username'];
	 $password   = $_POST['password'];
	 $strSQL = "SELECT * FROM ".USERS_TABLE_NAME."
			    WHERE username ='$username' AND password = '$password'
			   ";
        $result  = mysql_query ($strSQL); 
	$row     = mysql_fetch_row($result);
   
$_SESSION['user'] = $row['username'];
$_SESSION['id'] = $row['id'];

	$exist   = count($row);
	if($exist >=2) { $this->jscript_location();  } 
	else { $this->notify_show();}
	exit;		
  }   
  }

 

also tried like this too

 

function is_login() {
      if(isset($_POST['username']))  {
	     $username   = $_POST['username'];
	 $password   = $_POST['password'];
	 $strSQL = "SELECT * FROM ".USERS_TABLE_NAME."
			    WHERE username ='$username' AND password = '$password'
			   ";
        $result  = mysql_query ($strSQL); 
	$row     = mysql_fetch_row($result);
   
	$exist   = count($row);
	if($exist >=2) { 
      session_start();
$_SESSION['user'] = $row['username'];
$_SESSION['id'] = $row['id'];
$this->jscript_location();  } 
	else { $this->notify_show();}
	exit;		
  }   
  }

 

but did not get there values, it sets empty session variables.

Link to comment
Share on other sites

Well the "oop" isn't designed the way i would design it...

 

However below the

private $notify_element  = null;

add this

private $UserName = null;
private $UserID  = null;

 

and add these into the class

  public function getUsername(){
    return $this->UserName;
  }
  public function getUserID(){
    return $this->UserID;
  }

 

Now replace your commented out code with this

    $this->UserName = $row['username'];
    $this->UserID = $row['id'];

 

 

okay now.. the problem with session is probably due to the fact you have output sent before the session_start()..

 

So now find where you call this class

something like this

 

$ajax = new ajaxLoginModule();

 

and below add this (for testing)

echo $ajax->getUsername();

 

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.