Archive for August 2008

Best WordPress bug

If you are more content, if you need are legal money, if your ideas is using wordpress then your site is crashed.I’m using wordpress too many sites. I known wordpress db is safe but i have 30,000+ content (15,000 published and ~15,000 scheduled.), result : wp-cron.php has crashed my site. Memory-Cpu %100 and crash.

I’m try wp super cache pluging and wp-cron+ plugin but negatif.

I don’t use wordpress

when i have too many scheduled content.

Authentication by Usb Flash Driver

This article is another method for authentication. General method is simple username and password required authentication. This method may be additional your auth. method. Authorization is using for a role based security.

Required Equipments:

Another usb flash driver. (It’s damaged or secured, it can saw a “my computer” only.)


Analysis

We are get the usb flash drive PNPDeviceID, if my drive is needed drive than condition has true else false. Checking usb flash drive is another advantage for user: User use a another computer. (Same Rooming Profile for Server Operating System).

WMI (Windows Managment Interface) : WMI has created windows 2000 and later windows os. (Win NT Based OS). It’s get more information on running system and checking and managing litle system object. Any query with WQL for WMI. Windows Xp has approximate 900 WMI object. If you see wmi tester then click start menu and click run, write wbemtest and click Ok. For more information click here.
Example: “SELECT * From Win32_LogicalFileAccess”,
“SELECT * From Win32_LoggedOnUser” … like sql query:)

Writing Code

Create a new console project in visual studio 2005/2008
Open program.cs and paste following method

        private static bool ScanNeededDiskDriver(string PNPDeviceID)
        {
            Console.WriteLine("Starting query");
            ManagementObjectSearcher mOSClass = new ManagementObjectSearcher();
            mOSClass.Query = new ObjectQuery("SELECT * From Win32_DiskDrive");

            Console.WriteLine("\tSarching disk driver.");
            foreach (ManagementObject mOClass in mOSClass.Get())
            {
                if (mOClass["PNPDeviceID"].ToString() == PNPDeviceID)
                {
                    Console.WriteLine("\t\tSucces. Needed driver was found.");
                    return true;
                }
            }
            Console.WriteLine("\t\tNeeded disk driver can not be found.\r\n\t\tPlease insert another driver.");
            return false;
        }

and paste following code in the main method

            if (ScanNeededDiskDriver(@"USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0"))
            {
               // to do ...
            }
            else
            {
               // to do ...
            }

If your needed driver has been plugged then method is return true else false. For more information ManagementObjectSearcher Class

Full Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace Usb
{
    public class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (ScanNeededDiskDriver(@"USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0"))
            {
               // to do ...
            }
            else
            {
               // to do ...
            }
        }

        private static bool ScanNeededDiskDriver(string PNPDeviceID)
        {
            Console.WriteLine("Starting query");
            ManagementObjectSearcher mOSClass = new ManagementObjectSearcher();
            mOSClass.Query = new ObjectQuery("SELECT * From Win32_DiskDrive");

            Console.WriteLine("\tSarching disk driver.");
            foreach (ManagementObject mOClass in mOSClass.Get())
            {
                if (mOClass["PNPDeviceID"].ToString() == PNPDeviceID)
                {
                    Console.WriteLine("\t\tSucces. Needed driver was found.");
                    return true;
                }
            }
            Console.WriteLine("\t\tNeeded disk driver can not be found.\r\n\t\tPlease insert another driver.");
            return false;
        }
    }
}

VB.NET

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management

Namespace Usb
    Public Class Program
         _
        Private Shared Sub Main(ByVal args As String())
                    ' to do ...
            If ScanNeededDiskDriver("USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0") Then
                    ' to do ...
            Else
            End If
        End Sub

        Private Shared Function ScanNeededDiskDriver(ByVal PNPDeviceID As String) As Boolean
            Console.WriteLine("Starting query")
            Dim mOSClass As New ManagementObjectSearcher()
            mOSClass.Query = New ObjectQuery("SELECT * From Win32_DiskDrive")

            Console.WriteLine("" & Chr(9) & "Sarching disk driver.")
            For Each mOClass As ManagementObject In mOSClass.[Get]()
                If mOClass("PNPDeviceID").ToString() = PNPDeviceID Then
                    Console.WriteLine("" & Chr(9) & "" & Chr(9) & "Succes. Needed driver was found.")
                    Return True
                End If
            Next
            Console.WriteLine("" & Chr(9) & "" & Chr(9) & "Needed disk driver can not be found." & Chr(13) & "" & Chr(10) & "" & Chr(9) & "" & Chr(9) & "Please insert another driver.")
            Return False
        End Function
    End Class
End Namespace

If ManagementObjectSearcher class can not be found then please add System.Managament.dll reference.

Have fun.


Recover loss mssql database

First : DBCC CHECKDB (’DatabaseName’) — this command is list of database errors

Second: DBCC CHECKDB (’DatabaseName’, REPAIR_REBUILD) — if first command is return errors than execute this command. This command is repair successful of database.

Third: DBCC CHECKDB (’DatabaseName’, REPAIR_ALLOW_DATA_LOSS) — if second command is return errors than you may use this command but this command is dangerious because it’s make data loss for minimum damage. Run this command when absolutely create a backup.


Encrypting Data

Mssql 2005 has provides a lot of encrption function: PassPhrase, synrommetric, asymmetric and encryption by certificate.
PassPhrase function is basicaly method for data encryption. It’s use e user defined decryption key.

Encrypting data by PassPhrase:

Declare @EncryptedData varbinary(MAX)
Select @EncryptedData = EncryptByPassPhrase('cenq', 'mypassword' )
Select @EncryptedData

Decrypting data by PassPhrase:

Declare @DecryptedData nvarchar(36)
Set @DecryptedData = Convert(VarChar(36),DecryptByPassPhrase('cenq', 0x010000000F11B98B5BC8D062F57F404B59C12B0084EF898ABFCC7477F47C99BCF1259C00))
Select @DecryptedData

Output:

mypassword

Converting UnixTime To DateTime

Unixtime is 32-bit integer. It’s a second counter. A problem is converting datetime to unix time on mssql server. This solution convert unixtime to datetime.

For a more information for Unix Time by wikipedia.

Declare @UnixTime int
Set @UnixTime = 1219366020 -- 08.22.2008 00:47:00
Select dateadd(ss,@UnixTime,'1970-01-01')

To Function:

Create Function fn$UnixTimeToDatetime(@UnixTime int)
Returns Datetime
Begin
Return dateadd(ss,@UnixTime,'1970-01-01')
End

Go


Usage:

Select dbo.fn$UnixTimeToDatetime(1219366020) As DateAndTime