Jump to content

Working With Binary Strings


neoform

Recommended Posts

INSERT INTO
system_users_stored_sessions
SET
session = 'm\0ý?¤NGIs!ðü\"„Ø¿æ„tü±I„Ùõòä]Îm”áÖ,o>¢¨ü^•-ÀdÓkD,¿-ü–¼–ïT',
posted_on = NOW(),
name = 'foo',
body = 'blop'

 

I'm attempting to store a whirlpool hash (as a binary string since it's half the length of the hex version that is the default output..  the problem is, the second char of this particular hash is a null char, which for one reason or another causes mysql to kill the field after the m on output. I can't tell if this is a mysql or php issue, but I'm learning towards it being a mysql issue. When I assemble the query in PHP i use mysqli_real_escape_string..  maybe it's not escaping all the chars properly?

 

CREATE TABLE `system_users_stored_sessions` (
  `session` binary(128) NOT NULL,
  `name` char(32) NOT NULL,
  `posted_on` datetime NOT NULL,
  `body` varchar(65000) NOT NULL,
  PRIMARY KEY  (`session`,`name`),
  KEY `posted_on` (`posted_on`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

Any ideas?

Link to comment
Share on other sites

I'm starting to think this might be a PHP bug..

 

class nsessions
{
	const SESSION_TIMEOUT = 1800; //seconds

	public $vars; //65000 chars max
	private $name; //32 chars max

	public function __construct($name)
	{
		$this->name = $name;

		if (strlen($this->name) > 32)
		{
			trigger_error(
				"Could not create session properly. 
				The name assigned to the session \"".$this->name."\" is too long. 
				It should not be more than 32 chars long.", 

				E_USER_ERROR
			);		
		}

		//this might be better off as a cron
		$GLOBALS['sql']->write("
			DELETE FROM
				system_users_stored_sessions
			WHERE
				posted_on < '".safe_string(make_mysql_timestamp(time() - self::SESSION_TIMEOUT))."'	
		");
	}

	public function get()
	{
		if ($session_info = mysqli_fetch_assoc($GLOBALS['sql']->read("
			SELECT
				session body
			FROM
				system_users_stored_sessions
			WHERE
				session = '".$GLOBALS['sql']->real_escape_string($GLOBALS['ref_code'])."'
			AND
				name = '".$GLOBALS['sql']->real_escape_string($this->name)."'
		")))
		{ $this->vars = unserialize($session_info['body']); }
		else
		{ $vars = false; }
	}

	public function set(&$vars)
	{			
		$this->vars = $vars;

		$GLOBALS['sql']->write("
			INSERT INTO
				system_users_stored_sessions
			SET
				session = '".$GLOBALS['sql']->real_escape_string($GLOBALS['ref_code'])."',
				posted_on = NOW(),
				name = '".$GLOBALS['sql']->real_escape_string($this->name)."',
				body = '".$GLOBALS['sql']->real_escape_string(serialize($this->vars))."'
		");
	}
}

Link to comment
Share on other sites

Nah, come to think of it, it can't be that..

 

There must be something wrong when it comes to the real_escape_string function.

 

When I applied addslashes() to the string it properly escaped the null char..

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.