custom session_handler broken with php7 (session_set_save_handler) - php

After updating to PHP7 I have some problems with my applications sessionhandling.
It doesn't seem to be a big problem but PHP throws this error everytime:
[18-Jun-2016 20:49:10 UTC] PHP Warning: session_decode(): Session is not active. You cannot decode session data in /var/www/app/phpsessionredis.php on line 90
The session_handler is nothing special. It stores JSONified sessiondata to redis etc.
class phpsessionredis implements \SessionHandlerInterface {
public function __construct( &$redis ) {
$this->__rc = $redis;
}
public function open($savePath, $sessionName) {
return true;
}
public function destroy($id) {
try { $this->__rc->del($id); }
catch (\RedisException $e) { return false; }
}
public function close() {
return true;
}
public function write($id, $data) {
session_decode($data); // throws an error
try{
$this->__rc->setex( $id, 3600, json_encode($_SESSION) );
} catch (\RedisException $e) { return false; }
return true;
}
public function read($id) {
try {
$r = $this->__rc
->multi()
->get($id)
->expire($id, 3600)
->exec();
} catch (\RedisException $e) { return false; }
$_SESSION = json_decode( $r[0], true );
if( isset( $_SESSION ) && ! empty( $_SESSION ) && $_SESSION != null ){
return session_encode();
}
return '';
}
public function gc($maxLifetime) {
return true;
}
}
$sessionhandler = new phpsessionredis( $redis );
session_set_save_handler($sessionhandler);
ob_start();
session_start();
Any help is welcome.

I've got the same issue when updating to PHP7.
You get that warning because session_decode() needs an active session, it will populate $_SESSION.
That's not needed, you only want to unserialize the data to be stored into Redis.
This is the best solution i've found.
You can use this class to unserialize the session.
<?php
class Session {
public static function unserialize($session_data) {
$method = ini_get("session.serialize_handler");
switch ($method) {
case "php":
return self::unserialize_php($session_data);
break;
case "php_binary":
return self::unserialize_phpbinary($session_data);
break;
default:
throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
}
}
private static function unserialize_php($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
if (!strstr(substr($session_data, $offset), "|")) {
throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
}
$pos = strpos($session_data, "|", $offset);
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
private static function unserialize_phpbinary($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
$num = ord($session_data[$offset]);
$offset += 1;
$varname = substr($session_data, $offset, $num);
$offset += $num;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
}
?>
Your write() will be:
public function write($id, $data) {
$session_data = Session::unserialize($data);
try{
$this->__rc->setex( $id, 3600, json_encode($session_data) );
} catch (\RedisException $e) { return false; }
return true;
}

I don't know if this should be handled as a correct answer but it is a solution that seems to work.
ini_set('session.serialize_handler', 'php_serialize');
With this option we don't need session_decode and can replace it with unserialze() within the write method.
public function write($id, $data) {
$data = unserialize($data);
/** do something **/
}
For me this looks like a workaround.

Related

Using functions in PHP Method

I have a db class that I've been using for years. I noticed something interesting today.
function example_function($key)
{
global $db; // db variable - class
$a_id = 1; // example
$admins = $db->table('admins')->where('id','=',$a_id)->order('id','desc')->limit(1)->get();
$admin_data = $admins['data'][0];
$return = $admin_data[$key];
return $return;
}
$id = example_function('id');
$new = $db->table('rooms')->where('id','=',$id)->order('id','desc')->limit(2)->get();
print_r($new);
//WORKS
$new = $db->table('rooms')->where('id','=',2)->order('id','desc')->limit(2)->get();
print_r($new);
//WORKS
$new = $db->table('rooms')->where('id','=',example_function('id'))->order('id','desc')->limit(2)->get();
print_r($new);
//NOT WORKING ?
->where('id','=',example_function('id'))->
When I use an external function in this part, things get messy.
But the following also works. When doing another operation with the db in the relevant function, things get messy.
function example_function($key)
{
return '1';
}
$new = $db->table('rooms')->where('id','=',example_function('id'))->order('id','desc')->limit(2)->get();
Is the problem in global usage? Or is the database class wrong?
DB CLASS
class DB
{
protected $connect;
protected $db_database = DB_DATABASE;
protected $db_host = DB_HOST;
protected $db_username = DB_USERNAME;
protected $db_password = DB_PASSWORD;
protected $db_name = DB_NAME;
protected $db_prefix = DB_PREFIX;
//parameters
protected $error = null;
protected $last_id = null;
protected $query = null;
protected $from = null;
protected $select = '*';
protected $where = null;
protected $group = null;
protected $order = null;
protected $limit = null;
protected $pagination_type = false;
protected $per_page = 0;
protected $get_arr = null;
function __construct()
{
try {
$this->connect = new PDO("{$this->db_database}:host={$this->db_host};dbname={$this->db_name};charset=utf8mb4", "{$this->db_username}", "{$this->db_password}");
$this->connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8mb4");
} catch ( PDOException $e ){
echo "<b>Veritabanı bağlantısı sağlanamadı! - Hata Kodu: </b>".$e->getMessage();
exit;
}
}
public function now($type="datetime")
{
switch($type)
{
case 'timestamp';
return time();
case 'date';
return date('-m-d');
case 'datetime';
return date('Y-m-d H:i:s');
default:
return date('Y-m-d H:i:s');
}
}
public function m_empty($data)
{
if($data=='' and $data!='0')
{
return true;
}
else
{
return false;
}
}
public function query($query,$arr=false)
{
$this->reset();
if($query!="")
{
try
{
$run = $this->connect->prepare($query);
if($arr)
{
if(is_array($arr))
{
$run->execute($arr);
return $run;
}
else
{
$run->execute(array($arr));
return $run;
}
}
else
{
$run->execute();
return $run;
}
} catch (PDOException $e) {
$this->error = $e->getMessage();
return false;
}
}
return false;
}
public function table($table)
{
$this->from = $this->db_prefix . $table;
return $this;
}
public function select($fields)
{
$this->select = $fields;
return $this;
}
public function where($field,$op='=',$values,$and="AND")
{
if(!$this->m_empty($field))
{
if(is_null($this->where))
{
$this->where="{$field} {$op} ?";
$this->get_arr[] = $values;
}
else
{
$this->where.=" {$and} {$field} {$op} ?";
$this->get_arr[] = $values;
}
}
return $this;
}
public function where_set($field,$op='=',$values,$and="AND")
{
if(!$this->m_empty($field) and !$this->m_empty($values))
{
if(is_null($this->where))
{
$this->where="{$field} {$op} {$values}";
}
else
{
$this->where.=" {$and} {$field} {$op} {$values}";
}
}
return $this;
}
public function group($fields)
{
if(is_null($this->group))
{
$this->group = $fields;
}
else
{
$this->group .=",{$fields}";
}
return $this;
}
public function order($field,$type="desc")
{
if(!is_null($field))
{
if(is_null($this->order))
{
if(strtolower($field)=="rand()")
{
$this->order = $field;
}
else
{
$this->order = $field.' '.strtoupper($type);
}
}
else
{
if(strtolower($field)=="rand()")
{
$this->order .=",{$fields}";
}
else
{
$this->order .= ','.$field.' '.strtoupper($type);
}
}
}
return $this;
}
public function limit($start,$end=null)
{
if(is_null($end))
{
if(is_numeric($start))
{
$this->limit = $start;
}
}
else
{
if(is_numeric($start) and is_numeric($end))
{
$this->limit = "{$start},{$end}";
}
}
return $this;
}
public function pagination($per_page)
{
if(is_numeric($per_page))
{
$this->per_page = $per_page;
$this->pagination_type = true;
}
return $this;
}
public function get()
{
$query = "SELECT {$this->select} FROM {$this->from}";
if(!is_null($this->where))
{
$query.=" where {$this->where}";
}
if(!is_null($this->group))
{
$query.=" group by {$this->group}";
}
if(!is_null($this->order))
{
$query.=" order by {$this->order}";
}
if($this->pagination_type)
{
$c_per_page = $this->per_page;
$c_arr = $this->get_arr;
$extra_query = preg_replace("#SELECT (.*?) FROM#","SELECT COUNT(*) as total_count FROM",$query);
$all = $this->query($extra_query,$this->get_arr);
if($all)
{
$total_count = $all->fetchAll(PDO::FETCH_ASSOC)[0]['total_count'];
$page_count = ceil($total_count/$c_per_page);
$current_page = (integer)m_u_g(DB_PAGINATION_GET) ? (integer)m_u_g(DB_PAGINATION_GET) : 1;
$current_limit=($current_page - 1) * $c_per_page;
$query = $query." limit {$current_limit},{$c_per_page}";
$current_rows = $this->query($query,$c_arr);
$data = $current_rows->fetchAll(PDO::FETCH_ASSOC);
$return = array();
$return['total_count'] = $total_count;
$return['current_count'] = count($data);
$return['total_page'] = $page_count;
$return['current_page'] = $current_page;
$return['data'] = $data;
return $return;
}
else
{
return false;
}
}
else
{
if(!is_null($this->limit))
{
$query.=" limit {$this->limit}";
}
$gets = $this->query($query,$this->get_arr);
if($gets)
{
$data = $gets->fetchAll(PDO::FETCH_ASSOC);
$return = array();
$return["total_count"] = count($data);
$return["data"] = $data;
return $return;
}
else
{
return false;
}
}
}
public function get_var($field)
{
if($this->m_empty($field))
{
return false;
}
else
{
$query = "SELECT {$field} FROM {$this->from}";
if(!is_null($this->where))
{
$query.=" where {$this->where}";
}
if(!is_null($this->order))
{
$query.=" order by id desc limit 1";
}
$gets = $this->query($query,$this->get_arr);
if($gets)
{
return $gets->fetchAll(PDO::FETCH_ASSOC)[0][$field];
}
else
{
return false;
}
}
}
public function get_vars($table,$where,$var,$type=true)
{
if($type)
{
$data = $this->connect->query("select $var from $table where $where order by id desc limit 1")->fetch(PDO::FETCH_ASSOC);
}
else
{
$data = $this->connect->query("select $var from $table where $where")->fetch(PDO::FETCH_ASSOC);
}
return $data["$var"];
}
public function count()
{
$grouped = false;
$query = "SELECT count(*) as total_count FROM {$this->from}";
if(!is_null($this->where))
{
$query.=" where {$this->where}";
}
if(!is_null($this->group))
{
$grouped = true;
$query.=" group by {$this->group}";
}
if(!is_null($this->order))
{
$query.=" order by {$this->order}";
}
$gets = $this->query($query,$this->get_arr);
if($gets)
{
if($grouped)
{
$count = 0;
foreach($gets->fetchAll(PDO::FETCH_ASSOC) as $counts)
{
$count = $count+$counts["total_count"];
}
return $count;
}
else
{
return $gets->fetch(PDO::FETCH_ASSOC)["total_count"];
}
}
else
{
return false;
}
}
public function insert(array $data)
{
if(is_array($data))
{
$query = 'INSERT INTO '.$this->from;
$keys = array_keys($data);
$query.=' ('.implode(',',$keys).') values (';
$query_add='';
$values = array_values($data);
foreach($values as $val)
{
$query_add.='?,';
}
$query_add = trim($query_add,',');
$query.=$query_add.')';
if($this->query($query,$values))
{
$this->last_id = $this->connect->lastInsertId();
return $this->last_id;
}
else
{
return false;
}
}
return false;
}
public function update(array $data)
{
if(is_array($data))
{
$query = "UPDATE {$this->from} set";
$keys = array_keys($data);
$values = array_values($data);
$query_add = '';
foreach($keys as $key)
{
$query_add.=" {$key} = ?,";
}
$query_add = trim($query_add,',');
$query.=$query_add;
if(!is_null($this->where))
{
$query.=" where {$this->where}";
}
$new = array_merge($values,$this->get_arr);
if($this->query($query,$new))
{
return true;
}
else
{
return false;
}
}
return false;
}
public function delete()
{
$query = "DELETE FROM {$this->from}";
if(!is_null($this->where))
{
$query.=" where {$this->where}";
}
if($this->query($query,$this->get_arr))
{
return true;
}
else
{
return false;
}
}
public function error()
{
return $this->error;
}
public function last_id()
{
return $this->last_id;
}
protected function reset()
{
$this->error = null;
$this->last_id = null;
$this->query = null;
$this->from = null;
$this->select = '*';
$this->where = null;
$this->group = null;
$this->order = null;
$this->limit = null;
$this->pagination_type = false;
$this->per_page = 0;
$this->get_arr = null;
}
}
$db = new DB();
?>```
The issue is caused by changing the mutable object properties prior to completing the desired query with Db::get(). Resulting in the Db::$table property being changed when being called on the same instance of the Db class.
This is one of the reasons behind best-practices discouraging the use of global, since the intention/context of the current variable state is not easily determined.
What's happening with your code is
Db::$table = 'rooms'
Db::$table = 'admin'
Db::$where = 'admin.id = 1'
Db::get() //SELECT * FROM admin WHERE admin.id = 1
Db::$table = null
Db::$where = null
Db::$where = 'rooms.id = 1'
Db::get() //SELECT * FROM WHERE rooms.id = 1
To avoid the issue and ensure the desired order of operations, you need to call Db::get() prior to calling any other DB methods that modify the properties of the query issued when calling DB::get().
$roomId = example_function('id'); // (int) 1
//SELECT * FROM admin WHERE id = 1
$db->table('rooms')->where('id','=', $roomId)->get();
//SELECT * FROM rooms WHERE id = 1
Alternatively, to ensure that the intention of two individual queries is preserved, you would need to create a separate instance of DB for your second query. However, this would also create a separate PDO instance with your current code.
To avoid a second instance of PDO you would have to change $this->connect to a static property self::$connect and refactor any method using $this->connect to self::$connect. The static property will ensure that only a single instance of PDO will ever exist whenever new Db() is called.
class Db
{
protected static $connect;
//...
public function __construct()
{
try {
if (!isset(self::$connect)) {
self::$connect = new PDO("{$this->db_database}:host={$this->db_host};dbname={$this->db_name};charset=utf8mb4", "{$this->db_username}", "{$this->db_password}");
self::$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8mb4");
}
} catch ( PDOException $e ){
echo "<b>Veritabanı bağlantısı sağlanamadı! - Hata Kodu: </b>".$e->getMessage();
exit;
}
}
//...
}
Afterwards replace all usages of global $db; in your application with $db = new Db();.
This approach will also ensure your entire codebase is protected from the mutable object property overwrite issue and better adheres to best-practices.
function example_function($key)
{
$db = new Db();
$a_id = 1; // example
$admins = $db->table('admins')->where('id','=',$a_id)->order('id','desc')->limit(1)->get();
$admin_data = $admins['data'][0];
$return = $admin_data[$key];
return $return;
}
$new = $db->table('rooms')->where('id','=', example_function('id'))->get();
Another approach could be to use clone to workaround the mutable object issue. However, using clone will have adverse affects on the PDO instance, since it resides in the Db class. Most likely requiring you to separate the PDO connection code from the Query Builder code.
function example_function($key)
{
global $db;
$db2 = clone $db;
$a_id = 1; // example
$admins = $db2->table('admins')->where('id','=',$a_id)->order('id','desc')->limit(1)->get();
//...
}

Saving Array as JSON to a file using PHP

{"Files": [
{"file_name": "Text_file.txt","path": "to_folder","file_id": "abc12345"},
{"file_name": "Img.jpg","path": "to_folder","file_id": "abc12346"}
]}
I want to save information of file uploads to JSON file using php.
//Code: PHP class : Save()
<?php
class Save
{
private $file = './files/data.json', $jsonstring = null, $jsonarray = array(), $temp = array(), $data = array();
public function __construct()
{
$this->init();
}
private function init()
{
if (!file_exists($this->file)) {
touch($this->file);
$this->read();
} else {
$this->read();
}
}
private function read()
{
$this->jsonstring = file_get_contents($this->file);
$this->jsonarray = empty($this->jsonstring) ? array() : json_decode($this->jsonstring, true);
$this->temp = (object) $this->jsonarray;
}
private function write($data, $collection = false)
{
if ($collection) {
if (empty($this->jsonarray) || $this->jsonarray == null && $this->jsonarray[$collection] == null) {
unset($this->jsonarray);
$this->jsonarray = array();
$this->jsonarray[$collection] = array();
array_push($this->jsonarray[$collection], $data);
$this->jsonarray = json_encode($this->jsonarray);
file_put_contents($this->file, $this->jsonarray);
return 1;
} elseif (property_exists($this->temp, $collection)) {
// $this->jsonarray[$collection] = array_values($this->jsonarray[$collection]);
array_push($this->jsonarray[$collection], $data);
$this->jsonarray = json_encode($this->jsonarray);
file_put_contents($this->file, $this->jsonarray);
return 2;
} elseif (!property_exists($this->temp, $collection)) {
$this->jsonarray[$collection] = array();
array_push($this->jsonarray[$collection], $data);
$this->jsonarray = json_encode($this->jsonarray);
file_put_contents($this->file, $this->jsonarray);
return 3;
}
} else {
if (empty($this->jsonarray) || $this->jsonarray == null) {
unset($this->jsonarray);
$this->jsonarray = array();
array_push($this->jsonarray, $data);
$this->jsonarray = json_encode($this->jsonarray);
file_put_contents($this->file, $this->jsonarray);
return 4;
} else {
$this->jsonarray = array_values($this->jsonarray);
array_push($this->jsonarray, $data);
$this->jsonarray = json_encode($this->jsonarray);
file_put_contents($this->file, $this->jsonarray);
return 5;
}
}
return false;
}
public function push($data, $collection = false)
{
if (is_array($data)) {
$a =$this->write($data, $collection);
if ($a) {
return $a;
}
return false;
}
return false;
}
public function get($collection = false)
{
if ($collection) {
return json_encode($this->jsonarray[$collection]);
}
return json_encode($this->jsonarray);
}
}
Now Problem is when I upload a single file then it will work fine for 3-6 times then again it resets the JSON file because of null or error in JSON format. & when I Upload 30 files together sending each file automatically. JS code [...files].forEach(upload(file)) it behaves oddly. Here is the callback from write function
Update JS:
function handelSelect(e) {
let files;
if (e.type == 'drop') {
files = e.originalEvent.dataTransfer.files;
} else {
files = e.target.files;
}
if (files.length > 0) handleFiles(files);
}
//handelFiles(files) send files using ajax(single request).
Where am I doing it wrong?
After examining the execution time i came to conclusion:
Ajax Sending request way faster then PHP can write in file;
https://www.php.net/manual/en/function.file-put-contents.php
Benchmark below:
file_put_contents() for 1,000,000 writes - average of 3 benchmarks:
real 0m3.932s
user 0m2.487s
sys 0m1.437s
fopen() fwrite() for 1,000,000 writes, fclose() - average of 3 benchmarks:
real 0m2.265s
user 0m1.819s
sys 0m0.445s
so I delayed the request in javascript.
Object.keys(files).forEach((file) => {
setTimeout(() => {
uploadFile(files[file], file);
}, file*2500);
});
If anyone has a better solution please share.

warning in session_start() in php 7.1

my script has a problem and gives me this warning:
Warning: session_start(): Failed to read session data: user (path:
/Users/soroush/Documents/www/new/tmp/session) in
/Users/soroush/Documents/www/new/include/class.session.php on line 35
this is my class:
class SecureSessionHandler extends SessionHandler {
protected $key, $name, $cookie;
public function __construct($name = 'MY_SESSION', $cookie = [])
{
$this->key = SESSION_KEY;
$this->name = $name;
$this->cookie = $cookie;
$this->cookie += [
'lifetime' => 0,
'path' => ini_get('session.cookie_path'),
'domain' => ini_get('session.cookie_domain'),
'secure' => isset($_SERVER['HTTPS']),
'httponly' => true
];
$this->setup();
}
private function setup()
{
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
session_name($this->name);
session_set_cookie_params(
$this->cookie['lifetime'],
$this->cookie['path'],
$this->cookie['domain'],
$this->cookie['secure'],
$this->cookie['httponly']
);
}
public function start()
{
if (session_id() === '') {
if (session_start()) {
return mt_rand(0, 4) === 0 ? $this->refresh() : true; // 1/5
}
}
return false;
}
public function forget()
{
if (session_id() === '') {
return false;
}
$_SESSION = [];
setcookie(
$this->name,
'',
time() - 42000,
$this->cookie['path'],
$this->cookie['domain'],
$this->cookie['secure'],
$this->cookie['httponly']
);
return session_destroy();
}
public function refresh()
{
return session_regenerate_id(true);
}
public function read($id)
{
$data = openssl_decrypt(parent::read($id),'AES256',$this->key);
return $data;
}
public function write($id, $data)
{
return parent::write($id, openssl_encrypt(parent::read($id),'AES256',$this->key));
}
public function isExpired($ttl = 30)
{
$last = isset($_SESSION['_last_activity'])
? $_SESSION['_last_activity']
: false;
if ($last !== false && time() - $last > $ttl * 60) {
return true;
}
$_SESSION['_last_activity'] = time();
return false;
}
public function isFingerprint()
{
$hash = md5(
$_SERVER['HTTP_USER_AGENT'] .
(ip2long($_SERVER['REMOTE_ADDR']) & ip2long('255.255.0.0'))
);
if (isset($_SESSION['_fingerprint'])) {
return $_SESSION['_fingerprint'] === $hash;
}
$_SESSION['_fingerprint'] = $hash;
return true;
}
public function isValid()
{
return ! $this->isExpired() && $this->isFingerprint();
}
public function get($name)
{
$parsed = explode('.', $name);
$result = $_SESSION;
while ($parsed) {
$next = array_shift($parsed);
if (isset($result[$next])) {
$result = $result[$next];
} else {
return null;
}
}
return $result;
}
public function put($name, $value)
{
$parsed = explode('.', $name);
$session =& $_SESSION;
while (count($parsed) > 1) {
$next = array_shift($parsed);
if ( ! isset($session[$next]) || ! is_array($session[$next])) {
$session[$next] = [];
}
$session =& $session[$next];
}
$session[array_shift($parsed)] = $value;
}
}
and i use this class like this:
$session = new SecureSessionHandler();
ini_set('session.save_handler', 'files');
session_set_save_handler($session, true);
session_save_path(SITE_PATH.'tmp/session');
$session->start();
if ( ! $session->isValid(5)) {
$session->destroy();
die();
}
$session->put('site.langu', $lang->lang);
but it give me a warning in php 7.1
i use a mac os system and do this works
give 0777 permission to session folder
change chown with php running group and user
what should I do to fix this warning?
i solved this problem with this change:
public function read($id)
{
return (string)openssl_decrypt (parent::read($id) , "aes-256-cbc", $this->key);
}
public function write($id, $data)
{
return parent::write($id, openssl_encrypt($data, "aes-256-cbc", $this->key));
}
viva google :))

php class _Construct empty

<?
session_start();
/*
echo $_SESSION['SQLIP'];
echo "<br>";
echo $_SESSION['SQLDB'];
echo "<br>";
echo $_SESSION['SQLUSER'];
echo "<br>";
echo $_SESSION['SQLPASS'];
echo "<br>";
*/
class DB_MSSQL {
private $Host;
private $Database;
private $User;
private $Password;
public $Link_ID = 0;
public $Query_ID = 0;
public $Record = array();
public $Row = 0;
public $Errno = 0;
public $Error = "";
public $Halt_On_Error = "yes";
public $Auto_Free = 1;
public $PConnect = 0;
public function _construct(){
$this->Host = $_SESSION['SQLIP'];
$this->Database = $_SESSION['SQLDB'];
$this->User = $_SESSION['SQLUSER'];
$this->Password = $_SESSION['SQLPASS'];
}
function DB_MSSQL($query = "") {
if($query) {
$this->query($query);
}
}
function connect() {
if ( 0 == $this->Link_ID ) {
if(!$this->PConnect) {
$this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
} else {
$this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
}
if (!$this->Link_ID)
$this->connect_failed("connect ($this->Host, $this->User, \$Password) failed");
else
if (!mssql_select_db($this->Database, $this->Link_ID)) {
$this->connect_failed("cannot use database ".$this->Database);
}
}
}
function connect_failed($message) {
$this->Halt_On_Error = "yes";
$this->halt($message);
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->Link_ID)
$this->connect();
// printf("<br>Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ");
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info->name;
$res[$i]["len"] = $info->max_length;
$res[$i]["flags"] = $info->numeric;
}
$this->free_result();
return $res;
}
function affected_rows() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select ##rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
if ("no" == $this->Halt_On_Error)
return;
$this->haltmsg($msg);
if ("report" != $this->Halt_On_Error)
die("Session halted.");
}
function haltmsg($msg) {
printf("<p>Server have a critical error!<br><br><br>We are very sorry for any inconvenience!<br><br>\n", $msg);
printf("<b>MSSQL Error</b>: %s (%s)</p>\n",
$this->Errno,
$this->Error);
}
}
$_php_major_version = substr(phpversion(), 0, 1);
if((4 > $_php_major_version) or !class_exists("DB_Sql"))
{
class DB_Sql extends DB_MSSQL
{
function DB_Sql($query = "")
{
$this->DB_MSSQL($query);
}
}
}
unset($_php_major_version);
?>
I have a question, why on DB_MSSQL my $Host,$Datebase,$User,$Password are empty?
If i test $_SESSIONS Between DB_MSSQL are OK.
Class don't have errors but this variables are empty... and i don't know why..
Can anybody help me?
Thank you verry much!
You have missed one underscore, you should have write :
public function __construct()
It should work like this.

what is stream_wrapper_register() porotcol scope?

Hi
I am wondering what is the scope of custom wrapper created by this function stream_wrapper_register(). The reason I am asking is because I created a custom wrapper but whenever I try to include contents inside a function they are not displayed.
Here is my code:
<?php
class VariableStream {
var $position;
var $varname;
function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$this->varname = $url["host"];
$this->position = 0;
return true;
}
function stream_read($count)
{
$ret = substr($GLOBALS[$this->varname], $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
function stream_write($data)
{
$left = substr($GLOBALS[$this->varname], 0, $this->position);
$right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
$GLOBALS[$this->varname] = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}
function stream_tell()
{
return $this->position;
}
function stream_eof()
{
return $this->position >= strlen($GLOBALS[$this->varname]);
}
function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
$this->position = strlen($GLOBALS[$this->varname]) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
}
stream_wrapper_register("var", "VariableStream")
or die("Failed to register protocol");
$myvar = "Test contents";
include("var://myvar");
?>
This code works fine but when I change the last section to this:
function test()
{
$myvar = "Test contents";
include("var://myvar");
}
test();
The contents are not displayed.
This stream wrapper operates in the global scope.
$GLOBALS[$this->varname]
With this syntax it can only ever access the named variable as global. Thus it cannot access any local variables like in your test() function.
I see no immediate option to make this stream wrapper access e.g. get_defined_vars(). You'll have to assign a unique name and make it global.
You'll have to check if this setting is authorized in your php.ini.
allow_url_include.

Categories