Archive for June, 2007

Mysql fetch array problem

Sunday, June 24th, 2007

Currently I’m working on converting my practice project, a simple uploader program in PHP, into OOP.
Let’s take a look at the construct of the User class. For every instance of the class that is created, there is a check on whether the parameter provided is an id or a username, simply by checking if it’s a string or an integer. If it’s an integer it supposes that the variable given is a user id, and so continues by filling up the other properties.

class User {
 
private $mId;
private $mName;
......
 
function __construct( $construct ) {
 
  if ( is_int( $construct ) ) {  // mySQL query by user id
 
    $this->mId = $construct;
    .......
		
  else {                            // mySQL query by username
 
    $this->mName = $construct;
     .......

In order to display the username which is a private property ($mName) I used a simple fuction

public function GetName() {
 
return $this->mName;
}

So in my index.php I used to following code to display the user who owned a specific folder:

$owner = New User( $folder_owner['id'] );

$folder_owner['id'] was part of an mySQL fetched array which carried the owner’s id.

However, when I called to my previous method to display the username:

echo $owner->GetName();

the result was echoing not the username as expected but instead the user’s id.
foo.txt, created on 12-06-2007 by 2

The class’s methods seem correct, so what was it that went wrong back there?
Let’s look again the creation of my new User object.
$owner = New User( $folder_owner['id'] );

As I said before, $folder_owner['id'] came from a mysql_fetch_array function. When this function is called, the array created from each row is always an array of strings. So even if the cell that contains the requested id in my db contains an integer, the result will always be extracted as a string. As my constructor received a string and not an integer, $this->mName was set with the variable provided, which in our case was in fact the id and not the username, but still had the form of a string. So, calling at the method GetName() would always return the id.

The error was pointed out by my friend and tutor Dionyziz, who also suggested me the simple solution of converting $folder_owner['id'] to an integer before using it as a parameter for my method.

$owner = New User( ( integer ) $folder_owner['id'] );

What will definitely not be forgotten:

  • mysql_fetch_array() always returns an array of strings.

special thanks to Dionyziz for helping me out :)

More info:

mysql_fetch_array()

Unexplainable Compiling Errors

Saturday, June 23rd, 2007

We all agree that compiling errors are something very common while writing a program. Some of them can be detected by the compiler (mainly syntax errors) and some of them only during runtime. I thought the runtime errors were the most irritating and hard to detect ones until I got into this:

//graph.cpp
#include <iostream>
using namespace std;
 
int main() {
//----------Adjacency Matrix-------\\
	int matrix[2][2];
	matrix[0][0] = 0;
	matrix[0][1] = 1;
	matrix[1][0] = 1;
	matrix[1][1] = 1;
//----------------------------------\\
 
// code was continued here..
 
	return 0;
}

g++ graph.cpp
graph.cpp: In function ‘int main()’:
graph.cpp:8: error: ‘matrix’ was not declared in this scope

I would probably think that g++ had some kind of problem or I would rewrite the code from the start if I didn’t recall that something similar had occured to abresas. In a perfectly written code the compiler produced strange errors. He had also told me that he solved the problem by removing the comments! We couldn’t understand what exactly was going back then, but there is always a simple logical explanation for everything.

Do you remember a rule that everyone should follow while writing macros? The macros should all be in one line. That would make the code ugly though, so the following code:

#define ASSERT(x)if(! (x) ) {cout << "ERROR! Assert " << #x << "failed\n";cout << " on line " << __LINE__ << '\n';cout << " in file " << __FILE__ << '\n';} 

could also be written in this way:

#define ASSERT(x)\
 
        if(! (x) ) {\
 
             cout << "ERROR! Assert " << #x << "failed\n";\
 
             cout << " on line " << __LINE__ << '\n';\
 
             cout << " in file " << __FILE__ << '\n';\
 
        }

Apparently, ‘\’ is used for connecting lines apart from being an escape character.Therefore:

int main() {
//----------Adjacency Matrix-------\\
	int matrix[2][2];
	matrix[0][0] = 0;

is interpreted by the compiler as:

int main() {
//----------Adjacency Matrix-------\\int matrix[2][2];
	matrix[0][0] = 0;

and thus leaves us with an undefined variable.
Imagine having to face such an error while being on a contest. Yikes!

Disable Direct Root Login

Saturday, June 23rd, 2007

Most people use SSH to access their server over the Internet and administrate it. SSH is claimed to be a secure way of accessing a server. But with the wrong configuration this can be the contrary.

Disabling the direct root login is good as a hacker needs to guess 2 seperate passwords. So if he guesses the password of your first account he only has restricted access to your machine and needs to ‘su’ to gain root access, too.

First of all you should choose a good password for your users.

1. Let’s SSH into your server. You should use a second user account and ‘su’ to gain root access.

2. Open /etc/ssh/sshd_config with the editor of your choice.

vi /etc/ssh/sshd_config

3. For more security we should disable the insecure SSH Protocol 1. For doing so find the line ‘Protocol 2, 1′ and change it to ‘Protocol 2′.

4. Next thing is disabling the direct root login. The line ‘PermitRootLogin yes’ is allowing direct root logins. We need to change it to ‘PermitRootLogin no’.

5. After making these changes save the file and quit your editor.

6. Now we need to restart the SSHd

/etc/rc.d/init.d/sshd restart

Attention:

If you make a mistake in your sshd_config file it can happen that the SSHd will not start again. So take care of what you are doing and don’t logout before you tried your changes. Else you’ll need someone with physically access to your server to repair SSHd’s config.

Odd empty textNodes

Monday, June 4th, 2007

Not long ago, as I walked through the valley of the shadow of DOM, trying to help a friend of mine in a script he was writing, I found out that many elements had more children nodes than I thought they should have. For example:

<html>
  <head>
    <title>Kamibu</title>
  </head>
  <body>
    <i>
      <b>a</b>
    </i>
  </body>
</html>

From the first look it seems like the ‘i’ element has only one child node, the ‘b’ element. Using the following code at my browser though:

alert(document.body.childNodes[1].childNodes.length);

the number 3 comes up! But how could it be? :S By trying to alert the nodeName of these children

alert(document.body.childNodes[1].childNodes[0].nodeName);
alert(document.body.childNodes[1].childNodes[1].nodeName);
alert(document.body.childNodes[1].childNodes[2].nodeName);

I got:

#text
B
#text

And the thing is getting even more strange when the following code returns empty pop-ups:

alert(document.body.childNodes[1].childNodes[0].nodeValue);
alert(document.body.childNodes[1].childNodes[2].nodeValue);

After a short discussion with dionyziz about this subject, he explained to me that the empty textNodes were actually the spaces that existed in the source code for it to be easily readable. Therefore, the following pieces of code are different:

<i>
 <b>a</b>
</i>

and
<i><b>a</b></i>

, since the ‘i’ element of the second one has only 1 child ‘b’.

This may result in unexplainable scripting errors when someone is trying to access a child element of a node and instead he accesses a blank text Node. The following short function can be used in order to determine whether a node is a textNode or not:

<script language='text/javascript'>
function isTextNode(node) {
	if(node.nodeType == 3) {
		return true;
	}
	return false;
}
</script>