Jump to content

Session Not Recongised


Go to solution Solved by Ch0cu3r,

Recommended Posts

I have a script that is not own Originally this written in PHP5 and a little in getting the session value am going to write paste all

login.php

<?php
	session_start();
	
	$error_page = "index.php?error=";
	$success_page = "../main/log.php";

	
	define("PPDIA_APP", 1);
	require_once "../models/db.php";
	require_once "../models/loginModel.php";
	require_once "../models/sessionModel.php";
	
	//Create a new instance of the loginHandler
	$login = new loginModel();
	$login->setDebug();
	
	$login->setCredentials($_POST['username'], $_POST['card_pin']);

	//Authenticate user
	$logged_in = $login->authenticateUser();

	if( $logged_in ) {
		sessionModel::init();
		sessionModel::setParam("user", $login->getDetails());
		
		$location = $success_page;
	}
	else {
		$error_page .= "Your username and pin combination is invalid";
		$location = $error_page;
	}
	
	exit();

	header("Location: {$location}#!/page_jamb utme");
?>

Next is sessionModel.php

<?php
	class sessionModel {
		public static $session_id = "9jsschoo_php_app";
		
		public static function init() {
			session_start();
			$_SESSION[sessionModel::$session_id] = new stdClass();
		}
		
		public static function setParam($param, $data) {
			$_SESSION[sessionModel::$session_id]->$param = $data;
		}
		
		public static function resetParam($param) {
			unset( $_SESSION[sessionModel::$session_id]->$param );
		}
		
		public static function getParam($param) {
			return $_SESSION[sessionModel::$session_id]->$param;
		}
	}

Next is loginModel.php

<?php
	class loginModel extends db
	{
		private $credentials;
		private $enc_pass;
		private $details;
		
		function loginModel() {
			parent::__construct();
			$credentials = new stdClass();
		}
		
		function setCredentials($username, $password) {
			$this->credentials->username = $username;
			$this->credentials->password = $password;
			$this->enc_pass = $this->encryptPass($password);
		}
		
		function authenticateUser() {
			$check = $this->fetchData("", "#__users", "username", $this->credentials->username, "password", $this->enc_pass);
			
			if( !empty($check) ) {
				$this->details = (object) $check[0];
				
				return true;
			}
			else {
				return false;
			}
		}
		
		function getDetails() {
			return $this->details;
		}
	}

This Last One Is Not Important db.php

<?php
//No direct Access
defined("PPDIA_APP") or die("Restricted Access"); 
	
class db
{
	private $ppdia_conn, $db_res, $md_pass, $db_name;
	private $table_prefix, $limit;
	
	function db()
	{
		$this->table_prefix = "";
		
		$this->ppdia_conn = new mysqli( "localhost", "root", "", "skul_solution" )
		or die ( "error" );
	}
	
	function setPrefix($pref) {
		$this->table_prefix = $pref;
	}

	function getResult()
	{
		return $this->ppdia_conn;
	}
	
	function setDebug()
	{
		$this->debug = true;
	}

	function getDebug( $input )
	{
		if( $this->debug )
		{
			print "<div style='background-color: white; color: red;'><hr />";
			
			if( is_array($input) || is_object($input) )
				print "<pre>" . print_r( $input, 1 ) . "</pre>";
			else
				print $input;
			
			print "<hr /></div>";
		}
	}
	
	function myarray( $array )
	{
		print "<pre>"; print_r( $array); print "</pre>";
	}
	
	function prepareInput( $input )
	{
		if( is_array( $input ) && !empty( $input ) )
		{
			$keys = array_keys( $input );
			foreach( $input as $i )
			{
				$k = $keys[$x];
				$prepared = $prepared = $this->prepareString($i);
				$input[$k] = $prepared;
				
				$x++;
			}
		}
		else if( is_string( $input ) ) {
			$input = $this->prepareString($input);
		}
		
		return $input;
	}
	
	function prepareString($string)
	{
		$prepared = $this->ppdia_conn->real_escape_string( stripslashes($string) );
		
		return $prepared;
	}
	
	function encryptPass( $pass )
	{
		$spice = 'p0pSyPeD1A_5p1cypa55';
        $site_name = "9jaschool_php";
        $hash = md5( "Password: " . $pass . $site_name . $spice );
        
		$mdpass = substr( $hash, 0, 17 ) . ":" . substr( $hash, 17, 33 ); //echo $mdpass;
		$this->getDebug( "<p>" . $mdpass . "</p>" );
		
		/*		
		 * For PHP >= 5.5.0
		 * $mdpass = apps::encryptString($pass);
		 */	

		//echo $pass . " is " . $mdpass;
		
		return $mdpass;
	}
	
	function insertInto( $table, $rows, $values )
	{
		$table = $this->getTable($table);

		if( is_string($rows) )
			$rows = explode( ",", $rows );
			
		if( is_string($values) )
			$values = explode( ",", $values );
			
		if( is_array($values) && is_array($rows) && !empty( $values ) )
		{
			$x = 0;
			$c = count($values);

			foreach( $values as $v )
			{
				$r = $rows[$x];
				if( $v && $r )
					$nuval_arr[] = "`". $r . "`='" . $this->prepareString($v) . "' ";
				else
					$nuval_arr[] = "`". $r . "`='' ";
				
				$x++;
			}

			$val = implode(", ", $nuval_arr);
		}
		
		$value_set = $val;

		$sql_query = "INSERT INTO `" . $table . "` SET " . $value_set;

		$cm = $this->ppdia_conn;
		$this->getDebug( $sql_query );
		//Insert the data into the database....
		$queried = $cm->query( $sql_query );
		
		if( $queried ) {
			$this->getDebug($cm->insert_id);
			return $cm->insert_id;
		}
		else {
			return false;
		}
	}

	function updateData( $type='string', $tab_name, $row, $data, $key1='', $value1='', $key2=NULL, $value2=NULL, $no_queries=5, $switch=1 )
	{
		$tab_name = $this->getTable($tab_name);

		$cm = $this->ppdia_conn;
		if ( $type=='string' )
		{
			if( !$key2 )
				$reg_q = "UPDATE `" . $tab_name. "` SET `" . $row . "` = '" . $data . "' WHERE `" . $key1 . "` = '" . $value1 . "';";
			else
				$reg_q = "UPDATE `" . $tab_name. "` SET `" . $row . "` = '" . $data . "' WHERE `" . $key1 . "` = '" . $value1 . "' 
				AND `" . $key2 . "` = '" . $value2 . "';";
			
		}
		else
		{
			foreach( $row as $ro ) $db_row[] = $ro;
			foreach( $data as $d ) $udata[] = $d;
			//print "<pre>"; print_r($udata); print"</pre>";

			$x = 0;
			foreach( $db_row as $key=>$r )
			{
				$sets[] = "`" . $r . "` = '" . $udata[$x] . "'";

				$x++;
			}

			if( !$key2 )
				$where = "WHERE `" . $key1 . "` = '" . $value1 . "';";
			else
				$where = "WHERE `" . $key1 . "` = '" . $value1 . "' AND `" . $key2 . "` = '" . $value2 . "';";

			$reg_q = "UPDATE `" . $tab_name. "` SET " . implode(", ", $sets) . " " . $where;
		}

		$this->getDebug($reg_q);

		$reg = $cm->query( $reg_q );
		$res = $reg->affected_rows;
		
		return $res;
	}
	
	function checkDb()
	{
		if ( !$this->ppdia_conn ) return "Could Not Connect to The Database";
		else return true;
	}
	
	function getType($data)
	{
		if( !is_numeric($data) )
			$data = "'" . $data . "'";

		return $data;
	}

	function getTable($tname)
	{
		return str_replace( "#__", $this->table_prefix, $tname );
	}
	
	function getTableCols($table)
	{
		$table_name = $this->getTable($table);
		
		$sql = "SELECT table_name, column_name, data_type, data_length
				FROM USER_TAB_COLUMNS
				WHERE table_name = '" . $table_name . "';";
		
		//Mysql specific..
		$sql = "SHOW FIELDS FROM `" . $table_name . "`;";
		
		return $this->customQuery($sql);
	}
	
	function setLimit($start, $length) {
		$this->limit = " LIMIT {$start}, {$length} ";
	}
	
	function fetchData( $data, $t_name, $row1=NULL, $rowdata1=NULL, $row2=NULL, $rowdata2=NULL, $switch=1 )	//fetch all data from a table.
	{
		$cm = $this->ppdia_conn;
		$t_name = $this->getTable($t_name);
		
		$rowdata1 = $this->getType($rowdata1);
		$rowdata2 = $this->getType($rowdata2);
		
		if ( !$data ) $data = '*';
		if( !$row1 ) $sql = "SELECT " . $data . " FROM `" . $t_name . "`{$this->limit};";
		
		
		else if ( !$row2 ) $sql = "SELECT " . $data . " FROM `" . $t_name . "` WHERE `" . $row1 . "`=" . $rowdata1 . "{$this->limit};";
		else $sql = "SELECT " . $data . " FROM `" . $t_name . "` WHERE `" . $row1 . "`=" . $rowdata1 . " AND `" . $row2 . "`=" . $rowdata2 . "{$this->limit};";
		$this->getDebug( $sql );
		$result = $cm->query( $sql );
		if($result)
		while( $res = $result->fetch_assoc() )
		{
			$rawdata = $res;
			if( !empty($rawdata) ) {
				$output[] = $rawdata;
			}
		}
		if( !$switch ) $result->close;
		
		$this->getDebug( $output );
		
		return $output;
	}
	
	function customQuery( $sql, $switch=1 )
	{
		$cm = $this->ppdia_conn;
		$sql = $this->getTable($sql);
		$this->getDebug( $sql );
		
		$result = $cm->query( $sql );
		
		if( $result && ( preg_match("/select/",strtolower($sql)) || preg_match('/show fields/',strtolower($sql)) ) )
		{
			if( preg_match("/from/",strtolower($sql)) )
			{
				$res = array();
				while( @$res[] = $result->fetch_assoc() ) {
					$output = $res;
				}
			}
		}
		
		$this->getDebug( $output );
		
		return $output;
	}
	
	function checkTable()
	{
		$result = $this->fetchData( "", $this->getTableName() );
		
		if( $result )
			return true;
		else
			return false;
	}
	
	function deleteData( $table, $key, $value )
	{
		$table = $this->getTable($table);
		$sql = "DELETE FROM `" . $table . "` WHERE `" . $key . "`='" . $value . "'";
		$this->getDebug( $sql );
		$deleted = $this->ppdia_conn->query($sql);
		return $this->ppdia_conn->affected_rows;
	}
}

Thanks I Just want the session to use on another file that need session to work may be

the file that need the session is below

<?php
// PHP Script Chat - coursesweb.net

define('MAXROWS', 30);             // Maximum number of rows registered for chat
define('CHATLINK', 1);             // allows links in texts (1), not allow (0)

// Here create the rooms for chat
// For more rooms, add lines with this syntax  $chatrooms[] = 'room_name';
$chatrooms = array();
$chatrooms[] = 'English';
$chatrooms[] = 'Government';
$chatrooms[] = 'Economics';
$chatrooms[] = 'Commerce';
$chatrooms[] = 'C.R.K';


// password used to empty chat rooms after this page is accessed with ?mod=admin
define('CADMPASS', 'adminpass');

/* For example, access in your browser
    http://domain/chatfiles/setchat.php?mod=admin
*/


// If you want than only the logged users to can add texts in chat, sets CHATADD to 0
// And sets $_SESSION['username'] with the session that your script uses to keep logged users
define('CHATADD', 0);
if(CHATADD !== 1) {
  if(isset($_SESSION["9jsschoo_php_app"])) define('CHATUSER', $_SESSION["9jsschoo_php_app"]);;
}

// Name of the directory in which are stored the TXT files for chat rooms
define('CHATDIR', 'chattxt');

include('texts.php');             // file with the texts for different languages
$lsite = $en_site;                // Gets the language for site

if(!headers_sent()) header('Content-type: text/html; charset=utf-8');         // header for utf-8

// include the class ChatSimple, and create objet from it
include('class.ChatSimple.php');
$chatS = new ChatSimple($chatrooms);

// if this page is accessed with mod=admin in URL, calls emptyChatRooms() method
if(isset($_GET['mod']) && $_GET['mod'] == 'admin') $chatS->emptyChatRooms();

this is the part where i have to put the session

 if(isset($_SESSION[""])) define('CHATUSER', $_SESSION[""]);;

files that i suppose to contain the session

Edited by IlaminiAyebatonyeDagogo
Link to comment
Share on other sites

I guess the question is you want to set the CHATUSER constant to the user's username?

 

If thats the case. Looking at your code when the user logs in successfully you are saving the users data stored in the database into the $_SESSION["9jsschoo_php_app"] variable.

 

Try using

if(isset($_SESSION["9jsschoo_php_app"])) define('CHATUSER', $_SESSION["9jsschoo_php_app"]['username']);

if that doesn't work then what is the output of

printf('<pre>%s</pre>', print_r($_SESSION["9jsschoo_php_app"], 1));
Edited by Ch0cu3r
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.