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.