Adrian Callaghan`s Blog

Work

session_start hangs when it used to work

by Adrian on Dec.13, 2011, under Programming, Work, Zend

I had a problem with my zend application hanging with a white screen for no reason.
I narrowed it down to when

1
$adminSession = new Zend_Session_Namespace('Admin');

was executed, this is just a session? so debugging further, confirmed that session_start() hangs when it used to work.

There are lots of explanations for this, but before you start modifying ini settings, just try this

1
2
3
session_write_close();
session_start();
die(print_r($_SESSION,true));

I found this resolved the problem completely.
I then just removed this debug-code and went back to developing my zend app.

VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Leave a Comment more...

database class php

by Adrian on Aug.08, 2011, under Programming, Work

Updated the database class

Usage:

1
2
3
4
5
6
 
// min instantiation
$dataBase = new DB('hostname','username','password','database');
 
// max instantiation
$dataBase = new DB('hostname','username','password','database',DEBUG_LEVEL,array('admin@domain.com','admin@domain2.com));

Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 
class DB{
 
	/* 
	 * DESCRIPTION:		Object class, Handles all database requests and returns content accordingly
	 * WRITER:			Adrian Callaghan 12,08,08      
	 * UPDATED: 		Adrian Callaghan 08,08,11
	 * 
	 * 
	 * errLvl 
	 * 		(0), none, 
	 * 		(1) show a user error, 
	 * 		(2) (1) + mysql error
	 * 		(3) (2) + show a full error (mysql errors), 
	 * 		(4) (3) + plus debugging 
	*/
 
 
	private $host, $usr, $pwd, $errLvl, $mySqlHost, $dataBase, $emailAddresses;
 
 
	public function __construct($host, $usr, $pwd, $db, $errLvl=0, $adminEmails=array()) {
 
		/*
		 * Sets up a new connection
		 */
 
		// MIN required
		$this->setHost($host);
		$this->setUsr($usr);
		$this->setPwd($pwd);
 
		$this->setErrLvl($errLvl); // sets defualt RTE
		$this->setEmailErrsTo($adminEmails);
 
		// connect
		$this->connect();
		$this->setDb($db);
 
		}
 	public function __destruct(){
 		/*
 		 * Close the connection
 		 */
 		$this->disconnect();
 	}
	protected function setHost($host='localhost'){
		/*
		 * Sets the hostname
		 */
		$this->host = $host;
	}	
 	protected function setUsr($usr=''){
		/*
		 * Sets the username
		 */
 		$this->usr = $usr;
	}	
	protected function setPwd($pwd=''){
		/*
		 * Sets the password
		 */
		$this->pwd = $pwd;
	}	
	protected function setErrLvl($errLvl=0){
		/*
		 * Sets the error reporting level
		 */
		$this->errLvl = $errLvl;
	}	
	protected function connect(){
		/*
		 * Connect to server
		 */
		$this->mySqlHost = @mysql_connect($this->host, $this->usr, $this->pwd);
		if (!$this->mySqlHost) {
			$this->error('Cannot connect to server',mysql_error());
		}	
 
	}
	protected function disconnect() {
		/*
		 * closes current connection
		 */ 
		@mysql_close($this->mySqlHost);
	}
	public function setDb($db){
		/*
		 * Set the database
		 */
		$this->dataBase = @mysql_select_db($db,$this->mySqlHost);
		if (!$this->dataBase) {
			$this->error('Cannot select database',mysql_error());
		}
	}
	public function setEmailErrsTo($email = array()){
 
		// ensure it is an array
		$email = !is_array($email) ? array($email) : $email;
 
		// if empty return
		if (empty($email)){
			return;
		}
 
		// assign the email addresses
		$this->emailAddresses = $email;
	}
 
 
 
 
	function getQuery($SQL){
		// executes a query
		$result = @mysql_query($SQL);
		if (!$result) {
			$this->error('Invalid query',mysql_error(),$SQL);
			}
		// create a 2D array of results
		$return = array();
		while ($row = mysql_fetch_assoc($result)) $return[] = $row;
		return $return;
		}
 
 
 
 
	function setQuery($SQL){
		// executes a query without returning any results, used for insert, create etc, returns the ID of the last auto-increment
		$result = @mysql_query($SQL);
		if (!$result) {
			$this->error('Invalid query',mysql_error(),$SQL);
			}
		$return = mysql_insert_id();
		return $return;
		}
 
 
 
 
	public function error($message, $mysqlErr='', $SQL=''){
 
		/*
		 * Handles errors
		 * 
		 */
 
		switch ($this->errLvl){
 
			case 0: break;
			case 1:	$mysqlErr=''; // kill the mysql output
			case 2:	$SQL=''; // kill the sql output
			case 3: 
			case 4: 
				echo "<h1 style='color:#444444;'>$message</h1><hr>";
				if ($mysqlErr!=='') {
					echo "<p><span style='color:#ff0000; font-weight:bold;'>Mysql: </span>$mysqlErr</p>";
				}
				if ($SQL!=='') {
					echo "<p><span style='color:#ff0000; font-weight:bold;'>Whole query: </span>$SQL</p>";
				}			
 
		}
 
 		$email = $this->generateEmail(array('Error'=>$message,'Mysql'=>$mysqlErr,'Sql'=>$SQL,'Err Level'=>$this->errLvl));
 
 		// three is for debugging
 		if ($this->errLvl>3){
			echo $email;
		}
 
		if (!empty($this->emailAddresses)){
 
			$sent = $this->sendEmails($email);
 
 			if ($sent) echo '<h2>The administrator has been notified</h2>';
			else {
				echo '<h2>Please notify the administrator';
				echo count($this->emailAddresses)>1 ? "'s " : " ";
				echo implode(' or ',$this->emailAddresses);
				echo '</h2>';
			}
 
		}
 
		exit();
	}	
	private function generateEmail($err=array()){
 
		$fields = array(
			'Server name'=>$_SERVER['SERVER_NAME'],
			'Query'=>$_SERVER['SCRIPT_FILENAME'].'?'.$_SERVER['QUERY_STRING'],
			'Ip'=>$_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT'],
			'@'=>date("D dS M,Y h:i a")
		);	
 
		$rtn = '<table bgcolor="#cccccc">';
		foreach (array_merge($fields, $err) AS $title=>$value){
			$rtn.="<tr><td bgcolor='#aaaaaa'>$title</td><td bgcolor='#bbbbbb'>$value</td></tr>";
		}
		return $rtn.'</table>';
 
	}
	public function sendEmails($reciptents=array()){
 
		// email message
		$Name = $_SERVER['SERVER_NAME']; //senders name 
		$email = "noreply@".$_SERVER['SERVER_NAME']; //senders e-mail adress 
 
		$subject = $_SERVER['SERVER_NAME']." Error"; //subject 
 
		$header = 'MIME-Version: 1.0' . "\r\n";
		$header.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";		
		$header.= "From: ". $Name . " <" . $email . ">\r\n"; 
 
		$success=false;
		foreach ($this->emailAddresses AS $admin_email){
			$sent = @mail($admin_email, $subject, $mail_body, $header);
			if ($sent) $success=true; // holds state for any email being succesfully sent
		}
		return $success;
	}
}
VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Leave a Comment more...

compile your own lamp stack with mysqli

by Adrian on Jun.16, 2011, under Programming, Work

Install LAMP with one line

One liner

sudo apt-get install lamp-server^

Add CURL and enable mod-rewrite

sudo apt-get install php5-curl
sudo a2enmod rewrite

To compile LAMP with mysqli and mysql

Assuming you have a fresh clean minimal install of ubuntu start by install gcc and wget

sudo apt-get gcc
sudo apt-get wget

Download apache and php and use filezilla or a similar SFTP client to upload them to the server (I was unable to find a direct link that didnt pass through a mirror), once uploaded move to the directory on the cmd line.

unpack apache

1
2
gzip -d httpd-2_x_NN.tar.gz
tar -xf httpd-2_x_NN.tar

unpack PHP

1
2
gzip -d php-NN.tar.gz
tar -xf php-NN.tar

Compile and install Apache

1
2
3
4
cd httpd-2_x_NN
./configure --enable-so
make
sudo make install

Compile PHP

1
2
cd ../php-NN
./configure --with-aspxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/bin/mysql_config  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd

This will fail with an error “error locating libxml2″ fix this with:

sudo apt-get install libxml2

Next the error below will appear

xml2 config not found Please check your libxml2 installation

Point your browser or ftp client at ftp://xmlsoft.org/libxml2/ and take note of the latest version for libxml2-2.6.28 and then grab it and install it from the server

1
2
3
4
5
6
sudo wget ftp://xmlsoft.org/libxml2/libxml2-2.X.XX.tar.gz
sudo tar -zxvf libxml2-2.X.XX.tar.gz
cd libxml2-2.X.XX/
./configure
make
sudo make install

Try to compile PHP again:

1
./configure --with-aspxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/bin/mysql_config  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd

next problem is Mysql

configure: error: Cannot find MySQL header files under /usr/bin/mysql_config

To fix this, Install mysql:

sudo apt-get install mysql-server

But now even though Mysql is found there are still no headers so first install php module for mysql:

sudo apt-get install php5-mysql

and add the header library

sudo apt-get install libmysqlclient15-dev

Now PHP should compile and install fine:

1
2
3
./configure --with-aspxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/bin/mysql_config  --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
make
make install

Set up an ini file for PHP:

sudo cp php.ini-development /usr/local/lib/php.ini

tell apache to load the module:

LoadModule php5_module modules/libphp5.so

You need to re/start apache

/usr/local/apache2/bin/apachectl start

personally I like to restart the entire server with changes this big

shutdown now -r

Thats it!

VN:F [1.9.9_1125]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!