Categories
Uncategorized

Capturing the username in WordPress for use in another webapp

Sharing authentication between a WordPress log and a co-resident webapp is handy, but can be a bit of a challenge.

Today I needed to find a way for my webapp to pick up my WordPress username in order to use that as a filter criterion in an SQL query.

A little searching and experimenting led me to the following snippet of code which I put in my WordPress header.php file, just below the  tag.

1
2
3
4
<?php
$user = wp_get_current_user();
setcookie("username", $use-->user_login);
?>

This drops a cookie called 'username' which I can retrieve in my PHP webapp

1
echo "Username cookie is  ".$_COOKIE["username"];

There is no doubt a more efficient way of doing this, like ensuring that it only happens once in the session or attaching it to the login code, which I will explore when time permits.  For the moment this works and I can get on with my webapp!

Later:

A look at the wp-login.php suggests that the place to put this in the login script is in the wp_signon() function of user.php. This approach would be much preferable to putting it in the header – there's no sense in dropping the cookie every time a page is loaded. I'll report back when I've had a chance to test this out.

Categories
Database MySQL

Resetting a MySQL Password

Resetting a MySQL root password (or other password, for that matter) is easy enough if you have admin access to the server.

First go to the command line (Start > Run > "cmd") and type "mysqladmin" – and see a bunch of output text, you're good to go. otherwise you'le need to locate mysqladmin.exe and navigate to that folder.

Once there, enter the following command:

mysqladmin -u root password NEWPASSWORD

and you're good to go!

Categories
MySQL

Importing a Spreadsheet or .CSV file into MySQL

Importing a spreadsheet, or its comma separated (CSV) equivalent, into a MySQL database can be enormously challenging and frustrating.

After many attempts at the solutions available through online searches, I determined that the most effective way was to save the spreadsheet in comma-separated format and write a PHP script to read the file row by row and insert the records in the database.

You'll need to adapt this script to your situation, the example below is keyed to the table I've been working with.
[sourcecode language="php"]
";
// ~~~~~~~~~~~~~~~~~~~~~~~~~ /create attribute list ~~~~~~~~~~~~~~~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~~~~~~~ PROCESS DATA LINES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
foreach($lines as $line_num => $line) {
 // trim the line – any whitespace can complicate life later
 $line = trim($line);

 // explode the line into an array, splitting it on the commmas in the csv.
 $values = explode(",",$line);
 // prepare the beginning of the INSERT statement
 $insert_statement = "INSERT INTO packages ($attribute_list) VALUES (";
 $insert_statement = "INSERT INTO packages (attribute_one,attribute_two,attribute_three,attribute_4,attribute_5) VALUES (";
 // now loop through the array, adding the elements to the values() section of the insert statement
 foreach ($values as &$value) {
  // according to
  //  http://bytes.com/groups/php/1146-insert-null-not-blank-into-mysql-php-script
  // this will insert null for empty elements
  if ($value) {
   $insert_statement .= "'" . mysql_escape_string($value) . "'";
  } else {
   $insert_statement .= "NULL"; // in the SQL query "NULL" will NOT be quoted
  }
  $insert_statement .= ",";
 }
 // now lop off the trailing string…
 $insert_statement = substr($insert_statement,0,-1);
 // … and close the statement
 $insert_statement .= ");";

 // now we'll either execute the statement or display it onscreen
 // (by default we only display onscreen for debugging – user must pass query string to execute)
 if ($_GET['execute'] == "1") {
  // if the execute instruction is passed in the query string, "?execute=true", execute the insert statement against the database
  $result = mysql_query($insert_statement);
  echo "Inserted record
";
 } else {
  // otherwise, display the insert statement onscreen for debug purposes
  echo $insert_statement;
  echo "
";
 }

}
// ~~~~~~~~~~~~~~~~~~~~~~~~~ /process data lines ~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
?>
[/sourcecode]

Categories
Uncategorized

Converting WordPress back to MD5 Authentication

WordPress introduced a new password hashing method in version 2.5.  For those who wished to piggyback authentication of another webapp onto WordPress, this can cause a problem.  This plugin will restore MD5 password hashing for wordpress.

This plugin reverts storage of user passwords back to using MD5 hashing instead of the newer phpass hashing introduced in 2.5.

It is useful for people who need to maintain MD5 as the hashing type due to sharing of the WordPress user tables with other applications which require it.

If some of your passwords are already converted to phpass, do not fear. The next time those users log in to WordPress their passwords will be converted back to MD5.

Categories
Joomla!

SEF URLs cause access problems in Joomla!

Frank Summers reports a solution for a problem which causes pages to become inaccessible on a Joomla! site when Search Engine Friendly URLs are turned on.

It seems to have something to do with permissions on the Joomla! install directory, with a workaround via the $line_site variable in configuration.php.

Further information is available through Frank's link above and at Joomla.org.