Simply Program

Simply programming - It’s a life style
Filed under Programming, Server

I am always on the lookout for a nice simple htpasswd generator and I found a pretty nice one.

http://www.htaccesstools.com/htpasswd-generator/

Enjoy!

Comments (0) Posted by sp on Thursday, May 22nd, 2008


Filed under Programming, PHP

Hey all,

So how can you read a user who just logged in with htacess?

CODE:
  1. $auth_username = $_SERVER['REMOTE_USER']

Make sure you have it right by checking phpinfo().

Comments (0) Posted by sp on Monday, May 12th, 2008


Filed under Programming, PHP

My problem - when a user logged into my website and then went to my "blog" area (which did not require any logging in) Wordpress would removed all the sessions I had set.

Why did it do this?

If we open wp-settings.php the first few lines of code completely remove all session information (as well as other).

CODE:
  1. function wp_unregister_GLOBALS() {
  2. if ( !ini_get('register_globals') )
  3. return;
  4.  
  5. if ( isset($_REQUEST['GLOBALS']) )
  6. die('GLOBALS overwrite attempt detected');
  7.  
  8. // Variables that shouldn't be unset
  9. $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
  10.  
  11. $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  12. foreach ( $input as $k => $v )
  13. if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
  14. $GLOBALS[$k] = NULL;
  15. unset($GLOBALS[$k]);
  16. }
  17. }

To fix this issue I did the following:

CODE:
  1. function wp_unregister_GLOBALS() {
  2. if ( !ini_get('register_globals') )
  3. return;
  4.  
  5. if ( isset($_REQUEST['GLOBALS']) )
  6. die('GLOBALS overwrite attempt detected');
  7.  
  8. // Variables that shouldn't be unset
  9. $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','_SESSION');
  10.  
  11. $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES);
  12. foreach ( $input as $k => $v )
  13. if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
  14. $GLOBALS[$k] = NULL;
  15. unset($GLOBALS[$k]);
  16. }
  17. }

Comments (0) Posted by sp on Thursday, May 8th, 2008


Filed under Programming, PHP

This is quite a unique little function. It came in handy when I had to check a "global" array holding all key elements already loaded.

CODE:
  1. $foo = array('test1'=>"This is test 1",
  2.  
  3. 'test2'=>"This is test 2");
  4.  
  5. $global_bar = array('test1'=>'yep its loaded');
  6.  
  7. foreach($foo as $fkey=>$f){
  8.  
  9. if(!array_key_exists($fkey, $global_bar)){
  10.  
  11. // not there - keep going.
  12.  
  13. }else{
  14.  
  15. // Yep its there.
  16.  
  17. }
  18.  
  19. }

Very simple example above - for more information check php.net: http://www.php.net/manual/en/function.array-key-exists.php

Comments (0) Posted by sp on Friday, April 18th, 2008


Filed under Programming, PHP

Well, here I was again doing a fast freelance job for a friend - He wanted a full functioning forum up and ready to go as fast as possible. I of course thought of the very nice free system - phpbb(http://phpbb.com). To make things interesting we had already put in place a custom made membership system, the phpbb forum would now have to integrate into our Registration and Login process seamlessly.

My first instinct was to go to the boards on PHPBB and do a quick search to find my solution - It turns out the solution is not fully laid out... Or atleast to me it didn't seem to be layed out.

So after about 30-45 minutes looking over threads and replies I decided to just look at how PHPBB initiated its sessions and cookies and go for it myself.

Here are my easy steps to accomplish this login / registration task (I have no error checking/hack prevention in place for my example, I don't want to do everything for you now :) )

STEP 1 - REGISTRATION

CODE:
  1. //Query to select the max number of users in the table
  2. $sql = "SELECT MAX(user_id) AS total FROM phpbb_users";
  3. $select=mysql_query($sql);
  4. $row=mysql_fetch_array($select);
  5.  
  6. //Taking the max number of records and adding 1 for the next user_id
  7. $user_id=$row['total']+1;
  8.  
  9. //Query to insert the basics into the users table.
  10. //NOTE - Take validation into your own hands, this is just an example.
  11. $sql = "INSERT INTO phpbb_users (user_id, username, user_regdate,
  12. user_password, user_email,user_active)
  13. VALUES ($user_id, '$username',".time().",'$password','$email','1')";
  14.  
  15. //Insert the user
  16. $insert=mysql_query($sql);
  17.  
  18. //Setup the users group so he/she has posting ability.
  19. //Why group_id=3?
  20. //On a basic PHPBB installation:
  21. // ID #1 = Visitor/Guest
  22. // ID #2 = Admin
  23. // ID #3 = Basic registered user
  24. //If you have a custom user group setup,
  25. //replace 3 with the id number.
  26. $group_id=3;
  27.  
  28. $sql = "INSERT INTO phpbb_user_group (user_id,
  29. group_id, user_pending)
  30. VALUES ($user_id, $group_id, 0)";
  31.  
  32. //Insert user into the correct group.
  33. $insert=mysql_query($sql);
  34.  
  35. //Add in your own registration for your own website here.

STEP 2 - LOGIN

CODE:
  1. //Place this on top of your login script
  2.  
  3. define("IN_LOGIN", true);
  4. define('IN_PHPBB', true);
  5.  
  6. //Be sure to update the root path to match your forum settings
  7. $phpbb_root_path = './forum/';
  8. include($phpbb_root_path . 'extension.inc');
  9. include($phpbb_root_path . 'common.'.$phpEx);

While checking for the username/password within your own system (and succeeding) enter these few lines of code.

CODE:
  1. $select=mysql_query("SELECT * FROM phpbb_users WHERE username='".$username."'");
  2. $row=mysql_fetch_array($select);
  3. $userdata = session_pagestart($user_ip, PAGE_LOGIN);
  4. $session_id = session_begin($row['user_id'],
  5. $user_ip, PAGE_INDEX, FALSE, $autologin, '0');

The above peace of code will use PHPBB's own session handler to create the cookies / sessions needed to log the user into the forum - as well as your own system.

Enjoy!

Comments (0) Posted by sp on Tuesday, November 13th, 2007


Filed under Programming, AIR

Adobes Apollo project has been provided a new breath of life, which so conveniently is named AIR (Adobe Integrated Runtime). Yes, the AIR project is the same as Apollo just with a few new (advanced) features.

The key feature to Apollo/AIR is the runtime enviorment as it allows you to take your Javascript, HTML, Flash, or Flex application and turn it into a light desktop application.

Why do I say "light"? Frankly, the AIR runtime does not handle its resources and memory access very well - hence any large system seems clunky, and slow to response.

Now, what can a light application do? What is the Runtime useful for at this stage in the game?

Lets take a real world example - Pownce.com...
"You can access all of your Pownce notes just on our website or you can download a small program for your computer.

The software is available right now for Windows and Mac users and will be available for Linux soon too. It takes advantage of a new technology from Adobe called AIR, which is super useful."

Screenshot

View larger image

Yes... Pownce has created a very simple IM type application and integrated it to work directly with their website.

Now, however cool this might be - Is it really practical to have another system like this? I personally believe not, Pownce will not steal any facebookers just because they have a cool AIR application.

In my opinion the largest contribution this runtime offers is the ability to instantly update your users / members of new content on your website. Imagine your user having an instant notification sent right to their desktop when a user has posted a reply to their topic? Many of you will say... so... thats email? Not exactly, when it comes to automated responses email has some flaws.

Over all AIR is not fully operational, its real world examples and applications are more experiments / cool toys instead of practical applications.

I do however believe this will be quite revolutionary and will more then likely become an effective way to deliver fresh content to your users as well as communicate your users content with your website(Instant desktop updates without c++!).

More to come as I am now developing a few "Practical" applications for my employers.

RESOURCES

Adobe AIR
Adobe Showcase
O2Apps.com

Comments (0) Posted by sp on Sunday, November 11th, 2007


Filed under Misc., Make you smile

Go trooper! Go trooper!

Comments (0) Posted by sp on Tuesday, October 30th, 2007


Filed under Misc.

Here is a little excerpt from the article - Very interesting.

"Can you imagine living for four centuries? A team of scientists from Bangor University's School of Ocean Sciences believe they have found an animal which did just that, a quahog clam, Arctica islandica, which was living and growing on the seabed in the cold waters off the north coast of Iceland for around 400 years.
When this animal was a juvenile, King James I replaced Queen Elizabeth I as English monarch, Shakespeare was writing his greatest plays Hamlet, Othello, King Lear and Macbeth and Giordano Bruno was burnt at the stake for espousing the view that the Sun rather than the Earth was the centre of the universe."

Read the full article

Comments (0) Posted by sp on Tuesday, October 30th, 2007


Filed under Misc., Make you smile

Is it a bad thing that thinking about PHP makes me happy? :)

Read this article and you will understand: Sources of optimism found in the brain.

Comments (0) Posted by sp on Sunday, October 28th, 2007


Filed under Programming, Javascript, CSS

Thank you google toolbar!!

Read more: http://www.htmldog.com/ptg/archives/000017.php

Fix: http://code.jenseng.com/google/

Enjoy ;)

Comments (0) Posted by sp on Sunday, October 28th, 2007