Sunday, July 31, 2011

Restart Apache in MAC OS X

Apache webserver service can be restarted using the following command in a Mac OS X Terminal window:

sudo /usr/sbin/apachectl restart

Today, when I used this command to restart apache , from terminal after making some changes in php.ini file. it resulted in an error:

+ apachectl: /usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument

Then I looked into line 82 of apachectl, the shell script was referring to the ULIMIT_MAX_FILES variable, which was set to:

ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"

Changing the line as follows fixed the problem:

ULIMIT_MAX_FILES=""


My MAC OS X version is 10.6.5.
Hope this helps!

Friday, July 29, 2011

Drupal reset super user (admin) password

To reset Admin password in Drupal7 , there is a drupal hash script to generate a password hash from plain text.

To set new password for user 'admin' to 'mynewpass', then first in the drupal root directory, run the script:
$ ./scripts/password-hash.sh 'mynewpass'

Then it will echo something like this:

password: mynewpass        hash: $S$CGM3hk.Fvl/pQlirfJmIQiXMOdifVR.wPoyT9e81ktxAStq7pmGK

then use following SQL to update the password:

UPDATE users SET pass='$S$CGM3hk.Fvl/pQlirfJmIQiXMOdifVR.wPoyT9e81ktxAStq7pmGK' where uid=1;
//Drupal use uid=1 for super user (Site administrator)
For drupal6 , output of md5('mynewpass') will work.

Hope this helps!

Saturday, July 16, 2011

Structs vs Classes C# and C++

Someone may argue:
in C++ Structs are largely redundant Why C# still have them & not discarded it like multiple inheritance ?

Here is the answer :

In C++, a struct and a class are pretty much the same thing.
The only difference is the default visibility level (public for structs, private for classes).

However, in C# structs and classes are different.
1) In C#, structs are value types (instances stored directly on the stack, or inline within heap-based objects), whereas classes are reference types (instances stored on the heap).Classes are accessed indirectly using reference.
2) Structs can implement interfaces but cannot inherit from structs or classes, .
3) Structs cannot have destructors.

we can say ......
A C# struct is much more like a C struct than a C++ struct.

Hope it make this concept pretty clear!

Thursday, July 7, 2011

get last inserted id from database table in Java, .NET , PHP

When we tried to retrieve a new record ID immediately after insertion,We did face bit trouble in getting that.

Different languages have different syntax/process of doing this.

Here is, How did I manage to get this done, in required projects:
For Java & PHP , we are using MySql dtabase, while in .NET we have used SQL server.

Java



//DBHelper , written for Database handling
DBHelper dbHelper = new DBHelper();
       Connection con = null;
       
String querySave = "";

       querySave = "INSERT INTO `DATABASE_NAME`.`TABLE_NAME` (`FIELD`) VALUES ( '"
               + VALUE_TO_BE_INSERTED + "');";
       Statement stmt = null;
       ResultSet rs = null;
       try {
//con is connection to database
           this.con = dbHelper.openDB(); //Here dbHelper is an instance of class,
           stmt = con.createStatement();
           int insertedRow = stmt.executeUpdate(querySave,
                   Statement.RETURN_GENERATED_KEYS);
           rs = stmt.getGeneratedKeys();
           int key = 0;
           if (rs.next()) {
               // Retrieve the auto generated key(s).
               key = rs.getInt(1);
               System.out.println("last inserted key is : " + key);

           }
catch (SQLException e) {
           e.printStackTrace();
       }
finally {
           dbHelper.closeDB();
           if (stmt != null) {
               stmt.close();
               stmt = null;
           }
           if (rs != null) {
               rs = null;
           }
           if (this.con != null) {
               this.con = null;
           }
       }


.NET



//dbManager - an object of one of the class from DATA Access Layer
  string sql = "INSERT INTO tgc_user_profile(firstName,emailId, password) VALUES(@firstName,@emailId,@password);" + "SELECT uid FROM tgc_user_profile WHERE uid = @@IDENTITY";

           try
           {
               dbManager.OpenConnection();

               dbManager.AddParameter("@firstName", name);
               dbManager.AddParameter("@emailId", email);
               dbManager.AddParameter("@password", password);

                ID = (int)dbManager.ExecuteScalar(sql, CommandType.Text);
               
       
           }

           catch (Exception ex)
           {
             
           }

           finally
           {
               dbManager.CloseConnection();
           }


PHP




$fName=mysql_real_escape_string($fName);
$email=mysql_real_escape_string($email);
$password=mysql_real_escape_string($password);
//mysql_real_escape_string to avoid sql injection attacks ,as we are using dynamic SQL query
$query_save = "INSERT INTO tgc_user_profile(firstName,emailId, password)
               VALUES ('".$fName."', '" .$email."', '" .$password ."')";
                             
             $_result = mysql_query($query_save);
            $ID = mysql_insert_id();


Hope this helps :)

Your valuable comments are appreciated !

Monday, July 4, 2011

ADO.NET : Preventing SQL injection attacks

Use Sqlparameter data-related class to help in preventing SQL injection attacks.

We should avoid Dynamic SQL like this

string strQry = "SELECT Count(*) FROM Users WHERE UserName='" +
txtUser.Text + "' AND Password='" + txtPassword.Text + "'";

because if user enter Username name value likely ' Or 1=1 -- then it return true what ever the value we have in password.

Sunday, July 3, 2011

Detect Client Operating System using javascript

Add this javascript code snippet:

<script>

alert(window.navigator.appVersion);

</script>