Saturday, August 25, 2007

Managing Multiple Simultaneous Sessions With PHP

The Issue:

Managing multiple simultaneous sessions with PHP is not a fun thing.

How does one go about creating a session that is unique to the browser window or tab? The goal is to freeze the session state.

Generally when a user signs in into a Control Panel type of environment -> screen elements are displayed -> the user clicks on the element and performs the action. If the user were to open multiple new windows or tabs, all of the assigned session variables _will be_ overwritten. What this results in, is a huge mess of what was opened previously and newly opened content.

One way to work around this problem is to disable the right-click. Prevent the user from opening new windows, prevent the user from clicking any of the mouse buttons other than the left click. This workaround requires the user to sign in into the Control Panel with the Internet Explorer browser. Any other browser would fly through the filters. Javascript must be enabled etc etc. Detecting the browser_type is the next issue. It is possible to spoof the HTTP User Agent and have the client report, spoof and copy any other client/app. The disadvantages on such an approach quickly add to the mix. A lot of the online banking portals seem to follow this approach.

The next solution involves renaming and creating new PHP sessions. I haven't found this to work too well. There were too many hoops involved.

The Solution:

The solution is to create a multi-dimensional array. Dynamically feed and call the multi-dimensional array based on the parent called.

Lets say you have five variables (parent vars) and need to register five sessions. Create a unique hash for each of the five variables, create a multi-dimensional array and call the hash in every GET and POST request. Its that simple!

// session_start();

// Select vars from db

$foo = generate_hash();

// Loop them {
$_SESSION[$foo]['type'] = $foo;
}

To call these sessions, the subsequent pages would need to call a GET request of the form, $type = hash_value

Wednesday, July 18, 2007

RSM database is corrupt and cannot be Rebuilt

RSM database is corrupt and cannot be Rebuilt

Windows XP - If the Removable Storage service is disabled, the Computer Management applet will create an event log alert with the error:

"
RSM database is corrupt and cannot be rebuilt
"

To resolve this issue, set the Removable Storage startup type to Manual or Automatic(services.msc)

Alternatively, rebuild the RSM database by:

1. Stop the RSM Service
2. Delete the RSM databases located at %SystemRoot%\System32\NtmsData
3. Restart the RSM Service

Tuesday, July 17, 2007

Google, Unable to login - Certificate Expired Error

Google, Unable to login - Certificate Expired Error

Logging in to any of google's services will display a certificate expired error. This issue will occur if your computer clock is out of sync and displays an incorrect date/time.

Internet Explorer will display the certificate error and the post login process will stall. A temporary workaround to the problem is to enter the correct date/time through the windows date/time applet located in the systray. Attempt the login again after the date/time has been corrected.

Generally, a date/time error occurs due to a dead CMOS battery.

This issue affects all of google's services that require a google account- gmail, gtalk, google accounts, orkut etc.

Further reading:
Google Support - I received an error message that said to check my computer's clock settings

Tar: Cannot write: Disk quota exceeded

Tar: Cannot write: Disk quota exceeded

When creating a tar archive, tar will display the annoying error:

"
sdaX: write failed, user block limit reached
tar: Cannot write: Disk quota exceeded
tar: Error is not recoverable: exiting now
"

To resolve this issue, check that:

1) You have two times the free disk space proportionate to the size of the archive you are creating
2) The server does not have restrictions on individual file sizes
3) Hsphere - Double the disk quota value by navigating to the FTP user area and look for the quota parameter.

Note: Why hsphere has two config areas (FTP and Disk Limits) for controlling disk quotas is beyond my understanding.

The FTP quota is the real disk quota that controls free space available on the filesystem.

Friday, June 22, 2007

Is your computer running out of RAM?

Windows Users, Howto know if your computer is running out of RAM/Memory:

Open the Windows Task Manager (ctrl + alt + delete). Note the stats below the PF history. (screenshot)




From the screenshot above:

Total Physical Memory - This is the total amout of RAM/Physical memory installed in the computer. Total amount of RAM installed = 1.5GB

Commit Charge - The maximum amount of space used by the pagefile = 1.2GB

Total Physical Memory (K) = 1562668 ~1.5GB
Commit Charge Peak value (K) = 1340072 ~1.2GB

* Divide (K) by 1048576 to convert value to GB

Total Free RAM/Memory = Total Physical Memory - Commit Charge Peak value
= 1.5 - 1.2
= 0.3GB (~307MB)

In this example, 307MB is the total free memory available under load. This number is acceptable. I still have room to run a few more apps.

IF your total free memory is in the negative, Windows _will_ swap contents to the pagefile instead of the memory. This causes applications to slow down and Windows runs like a pig. Perhaps its time for that long awaited RAM upgrade. ;-)


Linux, check the amount of free RAM available:

Run the command free -m at the shell prompt:

-sh-3.00$ free -m
total used free shared buffers cached
Mem: 1010 794 215 0 14 205
-/+ buffers/cache: 575 435
Swap: 2000 257 1743

Sunday, June 17, 2007

Getting Up to Speed with RaidRails, Ruby on Rails

This how-to assumes you have a basic understanding of Ruby and Rails.

Requirements:

Windows XP (I have tested the following in a Windows XP Pro SP2 environment)

XAMPP:
Install - Apache & MySQL
http://www.apachefriends.org/en/xampp-windows.html

Instant Rails:
http://rubyforge.org/frs/?group_id=904

Radrails:
http://www.aptana.com/download_radrails.php


1) Configure Rails and Rake path
Navigate to the Instant Rails directory.
Select the rails binary and rake.bat as shown below




2) Setup the Ruby Interpreter
Select ruby.exe located in the \bin\ dir




3) Start the MySQL server




4) Create a New Project - RadRails





5) Enter a Project Name





6) Hello World!




Ruby:
http://www.ruby-lang.org/en/

Ruby on Rails framework:
http://www.rubyonrails.org/

Ruby forge:
http://rubyforge.org/

Ruby Help and Docs:
http://www.ruby-doc.org/

Streamlined:
http://streamlinedframework.org:8079/trac/

IBM Fast-track your Web apps with Ruby on Rails:
http://www-128.ibm.com/developerworks/linux/library/l-rubyrails/

Thursday, May 24, 2007

PHP Fatal error: [] operator not supported for strings

PHP Fatal error: [] operator not supported for strings

PHP will throw up the error:

"Fatal error: [] operator not supported for strings"

if:

- The array variable eg, $foo[] has been set elsewhere as a string
- The array variable has already been set as an array elsewhere

The solution:

- Do not mix the same variable names between strings and arrays
- Do not create duplicate array names

PHP fopen Fails to Open Directory in Windows

fopen Fails to Open Directory in Windows

fopen — Opens file or URL


However, if the file is a directory, fopen will fail to open the directory in windows. This issue does not occur in Linux.

Windows Error:
Warning: fopen(c:\windows\): failed to open stream: Permission denied

Regardless of the permissions on the windows or any other directory, fopen will display the error.

PHP Code:

Windows:
$fh = fopen('c:\\windows\\', 'r');

Linux:
$fh = fopen('/home/test/', 'r');

fopen Fails to Open Directory in Windows

PHP INSERT an Array into MySQL

Inserting an array into MySQL while escaping the string with mysql_real_escape_string() will throw up the error:

"mysql_real_escape_string() expects parameter 1 to be string"

I haven't got down to research the real reason behind this issue. The obvious reason is that mysql_real_escape_string() escapes all characters in a string. To escape an array, I guess we'd need to loop the contents and return the escaped array.

The solution:

-- During INSERT or UPDATE

- Serialize the array before the INSERT or UPDATE query
$array_var[] = $some_data;

$serialized_array = serialize($array_var);

// mysql_real_escape_string() will now escape $serialized_array and insert/update without errors.

-- During SELECT

- Unserialize the array after the SELECT query is executed

$array_var = unserialize($get_all[$k]['array_var']);

// Print the array
print_r($array_var);

Tuesday, May 22, 2007

Logic to Check the Status of a URL

Open socket to host
Check socket status
Proceed to grab headers
Check headers status

IF the URL is a file or similar
Parse page and look for href patterns
For each URL
Open socket to host
Check socket status
Proceed to grab headers
Check headers status

End

// Local Filesystem - Perform integrity checks
// Local Filesystem - Checks on date attribute
// Local Filesystem - Replace domain with the localPath, check status

Monday, May 21, 2007

sudo vs su -

The "sudo" command allows users specified in a sudoers file which is usually located in the /etc directory to perform certain functions (again, as permitted by the sudoers file) that are normally reserved for the root user. The syntax would be something like:

sudo command

where command is normally limited to the root user. You may be prompted for your normal user password, and if the root user has given you permission (in the sudoers file) to perform that action, you can.

The "su" command is a "switch user" command. In its simplest form, typing "su" will prompt you for the root password and if given correctly you get root privileges. Typing "su -" and giving the correct password gives you root's privileges and environment. The "su" command can also be used to gain access to another "normal" user's account if you have that user's password. To do that you would type "su " where is a valid normal user on that system.

Monday, April 23, 2007

Slow MySQL Performance over a USB Bus

Slow MySQL Performance over a USB Bus

Parsing a 3gig MySQL dB with ~5 million datasets can be agonizingly slow. The bottleneck here being the USB bus.

I had to get MySQL running off an external USB HDD due to the heat generated on my notebook (Intel Core 2 Duo, SATA HDD).

The only advantage of this setup is the heat issue. As for the cons, there are plenty:

- USB connector can break the connection for some reason
- Slow USB bus
- USB external is powered through AC, brick needs to convert from AC -> DC
- The external IDE connector

Time consumed:
100 tables per week
~2.5 weeks
22/7, One 10-15 minute standby break before script execution

Filesystem: NTFS

dB dumped and imported to an ext3 filesystem

Monday, April 02, 2007

Send Attachments with Mutt

Use mutt to send an email through shell.
-a flag to attach an attachment
-s flag is the subject
echo -e creates the message in the body

#!/bin/bash
echo -e "This is the body message of the email" | mutt -a attachment.zip foo@recipient.com -s "This is the email subject"

Monday, March 19, 2007

Reset the TCP/IP Stack

Reset the TCP/IP Stack

Windows XP - Is the TCP/IP config acting up? Enter the command in a DOS box and this will hopefully clear the mess!

Note: Write down or screen capture DNS addresses, LAN configs and other variables. The reset will wipe out all network settings.

netsh int ip reset c:\resetlog.txt

Tuesday, March 13, 2007

GPRS Access Numbers

GPRS Access Numbers

*99#
*99***1#

Disable Data Execution Prevention (DEP)

Disable Data Execution Prevention (DEP)

Edit the BOOT.INI file in the root of the partition from which the computer boots.

  • Right-click on My Computer and select Properties.
  • Click on the Advanced tab.
  • Click on the third button—settings for system start options.
  • Click on the Edit button to edit the boot.ini file.
  • Carefully edit the /NoExecute=OptIn string and change it to AlwaysOff. This part at the end of the line should now read: /NoExecute=AlwaysOff
  • Save and close the dialog boxes by clicking on OK.

Alternative Method:

  • ATTRIB -S -H -R C:\BOOT.INI
  • NOTEPAD BOOT.INI
  • Carefully edit the /NoExecute=OptIn string and change OptIn to AlwaysOff. This part at the end of the line should now read: /NoExecute=AlwaysOff
  • Close the editor and resave the BOOT.INI file back to where it was.
  • ATTRIB +S +H +R C:\BOOT.INI
To disable DEP by modifying the Boot.ini file, change the /noexecute policy level to alwaysoff.

/NoExecute=AlwaysOff

Sunday, March 04, 2007

Shell Environment Variables

Shell Environment Variables

List the environment vars from a shell prompt:

-sh-3.00$ env

Tuesday, January 23, 2007

Linux Shell

Linux Shell

List Files and Directories without Color Coding

$ls --color=none -all


List Files with Auto-Refresh

while [ 1 ]; do clear; ls -allh; sleep 5; done


Generate and Check MD5

$md5sum FILE > md5sum.txt
$md5sum -c md5.txt

Generate the Date of the Form: January_01_YYYY.$ext

$NOW=`date +%B_%d_%a_%Y


Get the Total Size of a Directory and File

$du -hcs dir/dir
$du -h file.tar.gz


Search for Text Pattern in Files

$egrep "stuff.php" *

Split and Join Files

Split and Join Files

$split -b 450m file.tar.gz

With Prefix
$split -b 450m file.tar.gz PREFIX

Join Files
$cat xaa xab xac >> file.tar.gz

Split and Join Files

Friday, January 19, 2007

mysqldump: Couldn't execute SHOW TRIGGERS LIKE

mysqldump: Couldn't execute SHOW TRIGGERS LIKE

Importing a database with 251+ tables on Windows XP results in the following error:
mysqldump: Couldn't execute 'SHOW TRIGGERS LIKE table_name. Can't create
ate/write to file '\tmp\#sql_8d4_0.MYD' (Errcode: 17) (1)

This issue is due to the windows file handles.

[quote]
Please examine the server status variable "open_files_limit" and note that
mysqld requires two open files for each myisam table.

On my machine it looks like this:
mysql> SHOW VARIABLES LIKE 'Open_files_limit';
Variable_name Value
open_files_limit 1024

From the manual:
The number of files that the operating system allows mysqld to open. This is
the real value allowed by the system and might be different from the value you
gave using the --open-files-limit option to mysqld or mysqld_safe. The value is
0 on systems where MySQL can't change the number of open files.

When mysqldump tries to dump the files, it will first take a read lock on all
the tables in the database. That requires it to open all of them at the same
time. So if the number of available open files is low, this kind of error can
occur.

To make mysqldump avoid taking the read lock use --skip-lock-tables option. I
successfully used that to dump more tables than my system had file descriptors.

It should also be possible to put a smaller number of tables in each database or
only dump a selected number of tables at a time.

But that are workarounds, best thing is to increase the number of open files on
the system.
[/quote]

Source:
http://bugs.mysql.com/bug.php?id=17089


mysqldump: Couldn't execute SHOW TRIGGERS LIKE

Wednesday, January 17, 2007

CSV Invalid Field When Importing Into MySQL

CSV Invalid Field When Importing Into MySQL

Check that there are no funny characters such as single quotes or double quotes.
The import fields need to be specified correctly.
The CSV input file needs to be saved in the CSV format.


CSV Invalid Field When Importing Into MySQL

Tuesday, January 16, 2007

Restore a MySQL Database

Restore a MySQL Database

Through Shell:

$ mysql -uUserName -pPassword -hlocalhost DatabaseName < mysql_db.sql


Restore a MySQL Database

Backup a MySQL Database

Backup a MySQL Database

Through Shell:

Backup an entire database

$mysqldump -udatabase_user -p --host="host.com" --opt -f database_name > database_backup.sql

Backup through Windows:

c:\> mysqldump -udatabase_user -p --host="host.com" --skip-lock-tables database_name > database_backup.sql


Backup a table

$mysqldump -udatabase_user -p --host="host.com" --opt -f database_name table_name > database_table_backup.sql

-p prompts for a password after the above command is executed

--force, -f

Continue even if an SQL error occurs during a table dump.

One use for this option is to cause mysqldump to continue executing even when it encounters a view that has become invalid because the defintion refers to a table that has been dropped. Without --force, mysqldump exits with an error message. With --force, mysqldump prints the error message, but it also writes a SQL comment containing the view definition to the dump output and continues executing.

--opt

This option is shorthand; it is the same as specifying --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.

The --opt option is enabled by default. Use --skip-opt to disable it. See the discussion at the beginning of this section for information about selectively enabling or disabling certain of the options affected by --opt.

Backup a MySQL Database

Saturday, January 06, 2007

Places to Visit

Places to Visit

India:
Taj Mahal, Red Fort Delhi - Visited - 2006

Basilica Old Goa - UNESCO - Visited

Australia:
Sydney Opera House
http://en.wikipedia.org/wiki/Sydney_Opera_House

Australia Zoo

Egypt:
Egyptian Pyramids
http://en.wikipedia.org/wiki/Egyptian_pyramids


Germany:
Magdeburg Water Bridge
http://en.wikipedia.org/wiki/Magdeburg_Water_Bridge

Autobahn
http://en.wikipedia.org/wiki/Autobahn

France:
Eiffel Tower
http://en.wikipedia.org/wiki/Eiffel_Tower

USA:
Niagara Falls
http://en.wikipedia.org/wiki/Niagara_Falls

Hollywood
http://en.wikipedia.org/wiki/Hollywood

Disneyland
http://en.wikipedia.org/wiki/Disneyland

Places to Visit

Find and Replace a Matching String of Text

Find and Replace a Matching String of Text

This script will find all .html files (in the current directory and all subdirectories) and replace the string of text 123 with 456

#!/bin/bsh

for file in $(find . -type f -name '*.html')

do

cat $file |sed "s|123|456|g" > $file.new

mv -v $file.new $file

done

Prefix and escape double quotes with a backslash, (double quotes are used by SED)

#!/bin/bsh

for file in $(find . -type f -name '*.php')

do
cat $file |sed "s|include \"file.php\";|require_once (\'/path/file.php\');|g" > $file.new

mv -v $file.new $file
done


Find and Replace a Matching String of Text

Archive and Compress a Directory

Archive, ZIP/Unzip and Compress/Decompress a Directory

$ tar -zcvf archive.tar.gz sourcedirectory/

Exclude a directory(s) from the archive:

$ tar -zcvf archive.tar.gz sourcedirectory/ --exclude=exclude01-dir/* --exclude=exclude02-dir/subdir/*


ZIP

$ zip db.zip foo-file

$ zip -r dir.zip directory/

Extract contents of a GZ archive
$ gunzip archive.sql.gz

or

$ gzip -d archive.sql.gz


Extract contents of a TAR GZ Archive
$ tar -zxvf archive.tar.gz

Recursively Copy a Directory

Recursively Copy a Directory

cp -r source newdestination

Note: newdestination does not need to exist

Recursively Copy a Directory

Sunday, December 17, 2006

Firefox List of Installed Plugins

Firefox List of Installed Plugins

Last updated: Sun, 17 Dec 2006 16:23:01 GMT
User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0

Extensions (enabled: 7, disabled: 1):

Themes (3):

Plugins (9):

  • Adobe Acrobat
  • DivX Player Netscape Plugin
  • DivX® Web Player
  • Java(TM) 2 Platform Standard Edition 5.0 Update 6
  • Microsoft® DRM
  • Mozilla Default Plug-in
  • QuickTime Plug-in 7.1.3
  • Shockwave Flash
  • Windows Media Player Plug-in Dynamic Link Library
Firefox List of Installed Plugins

SPAM and the Catch All Feature

SPAM and the Catch All Feature

One of our domains had been receiving a huge amount of SPAM. The Catch All feature had been enabled due to the fact that usernames could be created on-the-fly and used without creating inboxes.

With this "feature" enabled, a spammer could load up a dictionary with a list of usernames and blast the domain with SPAM. The reason why all email would be received is because Catch All does not bounce non-existent addresses/usernames. Instead, it forwards all incoming email to a POP email address.

We disabled the feature today and voila! SPAM levels reduced by 90%!!

SPAM and the Catch All Feature

Thursday, December 14, 2006

Procedure to Name MySQL DB's

I've been using the following method as the naming convention to name DB's/tables.

DB name-> host_dbtitle

Table name-> dbtitle_tableTopic

Foreign keys are created across all tables and _not_ on the last relation only.