PHP Secure Sessions Class
When creating websites with multiple pages, sometimes it is a good idea to be passing information from page to page based on the current user. PHP holds a way of doing this called sessions. Sessions allow variables to be used across multiple pages while been set, modified and created. However, PHP sessions are not the most secure form of doing this.
In this tutorial we will look at a custom way of passing details from page to page without PHP sessions. To do this we will use cookies and a MySQL database. There will be hints throughout the tutorial on making classes/variables and functions or you can read the The full script is available at the end of this tutorial.
First, make a PHP file called “class.session.php”. This will contain all of the back end code for our class.
Start your class with a name of sessionsClass.
< ?php class sessionsClass { } ?>
How to find Fragmented SQL Indexes
Storing data in SQL databases is easy, efficient and easily accessible. However, maintenance of these databases is crucial to keeping their performance good. One feature which helps SQL server to deliver large amounts of data quickly are indexes. These indexes store the relevant markers for pieces of data based on their values. These indexes, while constantly been updated can get fragmented, just like files on a hard drive. These indexes will only perform well if they are properly maintained.
Below is a handy bit of code to find out which indexes are fragmented and by how much. Based on this script, it can be extended to keep a record of index fragmentation and either REORGANIZE or REBUILD indexes based on their fragmentation level at certain times of the day.
SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName, ind.name AS IndexName, indexstats.index_type_desc AS IndexType, indexstats.avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats INNER JOIN sys.indexes ind ON ind.object_id = indexstats.object_id AND ind.index_id = indexstats.index_id WHERE indexstats.avg_fragmentation_in_percent > 0 ORDER BY indexstats.avg_fragmentation_in_percent DESC
Serializing Exceptions in VB
No matter how hard you try to make an application completely bug and error free, at least one error will slip through the net. Catching them is relatively easy in .NET and many other languages with a try catch statement, but what you do with that error can be tricky, especially in production environments where debugging can’t occur.
The most efficient way of looking after errors on .NET applications is to store the whole exception object, rather than just the message, which can be very unhelpful at times. To do this easily, the storing of the object can be done in XML, and stored in a database, text file or even sent in an e-mail, that’s entirely up to you.
The method provided below (available in VB and C#) will take an exception and return a nicely formed XML string for all error details up to 3 levels deep.
Public Function GetExceptionSerialized(ByVal ex As Exception) As String ' Start XML string. Dim xmlString As String = "" ' See if we actually have an exception first. If ex IsNot Nothing Then xmlString = String.Format("<Exception>") ' See if we have a top level message. If ex.Message IsNot Nothing AndAlso ex.Message <> "" Then xmlString += String.Format("<Message>{0}</Message>", ex.Message.ToString()) xmlString += String.Format("<Source></Source>", ex.Source.ToString()) xmlString += String.Format("<StackTrace></StackTrace>", ex.StackTrace.ToString()) End If ' See if we have an inner exception. If ex.InnerException IsNot Nothing Then xmlString += String.Format("<InnerException>") If ex.InnerException.Message IsNot Nothing Then xmlString += String.Format("<Message>{0}</Message>", ex.InnerException.Message) xmlString += String.Format("<Source></Source>", ex.InnerException.Source.ToString()) xmlString += String.Format("<StackTrace></StackTrace>", ex.InnerException.StackTrace.ToString()) End If If ex.InnerException.InnerException IsNot Nothing Then xmlString += String.Format("<InnerException>") If ex.InnerException.InnerException.Message IsNot Nothing Then xmlString += String.Format("<Message>{0}</Message>", ex.InnerException.InnerException.Message) xmlString += String.Format("<Source></Source>", ex.InnerException.InnerException.Source.ToString()) xmlString += String.Format("<StackTrace></StackTrace>", ex.InnerException.InnerException.StackTrace.ToString()) End If xmlString += String.Format("</InnerException>") End If xmlString += String.Format("</InnerException>") End If xmlString = String.Format("</Exception>") End If ' Return the built string. Return xmlString End Function



