Archive for the 'PHP' Category...
Filed under Programming, PHP
When you start creating advanced applications that require different types of data that may be defined by a developer using your system you come across a very interesting issue.
Dynamic Variables...
They are quite easy to master, just look at the below example.
CODE:
-
$foo = 'howdy';
-
$$foo = 'not howdy';
-
-
echo $$foo; //should return not howdy
Learn more by going here:
http://www.php.net/manual/en/language.variables.variable.php
Filed under Programming, PHP
http://www.phpdoc.org/
Easy, Fast, and Efficient.
Just make sure your code is developed within a standard and you comment very well.
Filed under Programming, PHP
How do you show errors for a single php script when the entire server has the enviorment variable "display_errors" set to 0?
Simple - place the below code at the top of your script.
CODE:
-
ini_set("display_errors", 1);
-
error_reporting(E_ALL);
ini_set - allows you to mess with your php.ini file.
error_reporting - sets what kind of errors you wish to show.
Filed under Programming, PHP
My current employer is in need of a very simple advertisement plugin to be created for their WordPress system - so of course I have decided to use this as an opportunity to create a custom plugin for experimental reasons.
Plugin Title: Make Me Money
Lets see if it does haha.
Filed under Programming, PHP
Hey all,
So how can you read a user who just logged in with htacess?
CODE:
-
$auth_username = $_SERVER['REMOTE_USER']
Make sure you have it right by checking phpinfo().
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:
-
function wp_unregister_GLOBALS() {
-
if ( !ini_get('register_globals') )
-
return;
-
-
if ( isset($_REQUEST['GLOBALS']) )
-
die('GLOBALS overwrite attempt detected');
-
-
// Variables that shouldn't be unset
-
$noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
-
-
$input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
-
foreach ( $input as $k => $v )
-
if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
-
$GLOBALS[$k] = NULL;
-
unset($GLOBALS[$k]);
-
}
-
}
To fix this issue I did the following:
CODE:
-
function wp_unregister_GLOBALS() {
-
if ( !ini_get('register_globals') )
-
return;
-
-
if ( isset($_REQUEST['GLOBALS']) )
-
die('GLOBALS overwrite attempt detected');
-
-
// Variables that shouldn't be unset
-
$noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','_SESSION');
-
-
$input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES);
-
foreach ( $input as $k => $v )
-
if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
-
$GLOBALS[$k] = NULL;
-
unset($GLOBALS[$k]);
-
}
-
}
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:
-
$foo = array('test1'=>"This is test 1",
-
-
'test2'=>"This is test 2");
-
-
$global_bar = array('test1'=>'yep its loaded');
-
-
foreach($foo as $fkey=>$f){
-
-
if(!array_key_exists($fkey, $global_bar)){
-
-
// not there - keep going.
-
-
}else{
-
-
// Yep its there.
-
-
}
-
-
}
Very simple example above - for more information check php.net: http://www.php.net/manual/en/function.array-key-exists.php
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:
-
//Query to select the max number of users in the table
-
$sql = "SELECT MAX(user_id) AS total FROM phpbb_users";
-
$select=mysql_query($sql);
-
$row=mysql_fetch_array($select);
-
-
//Taking the max number of records and adding 1 for the next user_id
-
$user_id=$row['total']+1;
-
-
//Query to insert the basics into the users table.
-
//NOTE - Take validation into your own hands, this is just an example.
-
$sql = "INSERT INTO phpbb_users (user_id, username, user_regdate,
-
user_password, user_email,user_active)
-
VALUES ($user_id, '$username',".time().",'$password','$email','1')";
-
-
//Insert the user
-
$insert=mysql_query($sql);
-
-
//Setup the users group so he/she has posting ability.
-
//Why group_id=3?
-
//On a basic PHPBB installation:
-
// ID #1 = Visitor/Guest
-
// ID #2 = Admin
-
// ID #3 = Basic registered user
-
//If you have a custom user group setup,
-
//replace 3 with the id number.
-
$group_id=3;
-
-
$sql = "INSERT INTO phpbb_user_group (user_id,
-
group_id, user_pending)
-
VALUES ($user_id, $group_id, 0)";
-
-
//Insert user into the correct group.
-
$insert=mysql_query($sql);
-
-
//Add in your own registration for your own website here.
STEP 2 - LOGIN
CODE:
-
//Place this on top of your login script
-
-
define("IN_LOGIN", true);
-
define('IN_PHPBB', true);
-
-
//Be sure to update the root path to match your forum settings
-
$phpbb_root_path = './forum/';
-
include($phpbb_root_path . 'extension.inc');
-
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:
-
$select=mysql_query("SELECT * FROM phpbb_users WHERE username='".$username."'");
-
$row=mysql_fetch_array($select);
-
$userdata = session_pagestart($user_ip, PAGE_LOGIN);
-
$session_id = session_begin($row['user_id'],
-
$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!
Filed under Programming, ActionScript, PHP
Well - LJ has returned from Vacation and has finally launched the new "Flash" version of BuzzPlay.com.
Check it out here: http://www.buzzplay.com
Now, why am I posting about this? Simple - I did all the programming for the site. This was my first major Flash (ActionScript) job. I used it as a test to see how effective I am in ActionScript. I was able to developed the whole system within a couple days, it wasn't all that hard.
What was the hardest aspect of the site?
What a great question Aaron
That hardest part was when he wanted the user to "stay" logged into their account while moving to completly seperate pages on the site (which where all in flash). I figured this out useing loadVars.
When the user logged into their account I sent their information to a php script and created a session, on every other page I used AS to pull that session information and allowed me to pull their account on every page. Pretty nifty I think.
Heres the code I used - very basic and simple(HINT: Put in error checks and variable validation)
Actionscript:
-
var artistName="";
-
var myID=0;
-
var checkLogin = new LoadVars();
-
checkLogin.onLoad = function (success:Boolean){
-
if(success){
-
if(checkLogin.myID!=""){
-
_root.myID=checkLogin.myID;
-
gotoAndStop(2);
-
}else{
-
//gotoAndStop(2);
-
//LOGIN SCRIPTÂ - OR REDIRECT
-
-
}
-
}else{
-
//gotoAndStop(2);
-
//LOGIN SCRIPTÂ - OR REDIRECT
-
}
-
}
-
checkLogin.load("checkLogin.php");
-
stop();
Here is the PHP Code:
PHP:
-
//Include your db info and all that good stuff.(error checking)
-
-
-
echo "myID=".
$_SESSION[userId
];
-
-
?>
You can place the AS script in the first frame of all your files... works very well
Support Our Sponsors!