Tuesday, September 21, 2010

$_GET, $_POST, $_REQUEST, $_FILE Made Easy

This may not be a new invention or secret but am gonna share it any way:

INSTEAD OF ALL THESE;
$day = (isset($_REQUEST['birth_day'])) ?$_REQUEST['birth_day'] : NULL;

YOU CAN NOW WRITE JUST THIS;
$rq = new requesthandler;
$day = $rq->get('birth_day');


USING THIS CLASS;

class requesthandler {
function get( $data )
{
return (isset( $_GET[$data])) ? $_GET[$data] : NULL;
}

function post( $data )
{
return (isset( $_POSt[$data])) ? $_POST[$data] : NULL;
}

function request( $data )
{
return (isset( $_REQUEST[$data])) ? $_REQUEST[$data] : NULL;
}

/*
* @param:
* $data = $_FILES['file']['name']
* @ret:
* Array
*/
function files( $data )
{
return (isset( $data)) ? $data : NULL;
}
}


Always remember though to unset your classes to free the object from memory. its not that required but its a good practice when you are going on larger projects. use this to unset
unset( $rq );

Wednesday, September 15, 2010

Dealing with errors while using headers in php (Headers Already Sent...)

OK, So am on this new project and am using a lot of headers (it was necessary) and I started to get the headers already sent issue. if you are a good PHP programmer you will immediately advice I get "echo" and all other similar things out of my code but you know sometimes its very important. check the code below. its not part of the codes I was writing its just an example for y'all to see how it's necessary sometimes;

if (is_resource($sql)) {
echo "your search came through with these results";
} else {
header ("Location: something.php?msg=error");
}
?>

In the above example you are displaying success on the same page and error on another page "something.php" with these parameters "msg=error" which is just how this application is to run.
In this example you may have the headers already sent error because you are echoing before a header is called into your code.

here is how to do it safely to avoid the header things;


ob_start();
if (is_resource($sql)) {
echo "your search came through with these results";
} else {
header ("Location: something.php?msg=error");
}
ob_end_flush();
?>

by using ob_start(); you now turn buffering on which prevents other types of output other than the headers found in your code. find a better explanation on Open Buffer Functions in PHP here
http://www.php.net/manual/en/function.ob-start.php

here is something to consider; by calling ob_end_flush(); you are closing all buffers which means outputs from your echo will start to give you bugs again. so whatever you do if you are going to use headers and echos just make sure you ob_start() at the very begining of your code and ob_end_flush() at the end.

Sessions however is a different thing and they usually require staying on top. so you may wnat to put ob_start() second should you ever use the two.

some kinda experience this was huh? well I shared it anyway... how was yours like?

Monday, August 30, 2010

Google Translate in Ghana (Ewe Chronicles)

I couldn't attend day 2 of the Google Ghana translation exercises due to a meeting I had to attend elsewhere (Yes on a Sunday, what is your problem? my work is that tedious sometimes) so back to what I was saying;
My brother attended and from his report this is what my other comments and conclusions are;
Its just interesting how these things are going on. to be honest Ghana isn't the only country with such problems am not certain of Nanjala's (The Google Kenya rep) comment when she said "The translation for each language usually takes two days, it took you all one day and its good work, Congratulations!" hmmmmm well very encouraging and I would have probably made the same comment when I needed people for day 2. hahahahahhahaha (ok cut out the jokes) to the point, lets just say its normal we could have been extra ordinary though, by getting the right people for the job. maybe am taking this whole thing seriously like it was the world cup heheheh lets just skip it. enough of it right? Ghana has successfully translated all the required text and data presented by Google in Ga and Ewe and its waiting for scrutiny and approval. Good Job!
And the Akans, Greaaat Job, You are good to go and you are already gone. Am proud. lets just not get controlled lets control. its not about what they want cos in the end they will give them back to us. lets set the rules, lets not follow them cos we have not set them yet.
All thesame about the translation, lets just be better next time.... Gambatte Ghana, Gambatte!!!

Saturday, August 28, 2010

Shell-Storm.org #WarGame

Yea I signed up for the shell-storm annual hacking thing and Oh My God! am still on level1 and I just thought I blog what am doing which is practically nothing! hahahhahahahaha

am here attempting to break throught the servers and pass level1 at least level 1. the usually way to do these things is by Buffer overflow; take this example for instance which I got from Ivan who is 2nd with 55000 points. the competition is 12 hours and he is done 10 rounds less than 2 hours WTH?
anyway here is the file; and please be mindful these things I blog here are for educational and professional purposes only, I do not expect you to use them for harm. should you do that I disclaim, denounce, disassociate, dishonor, dismount and dis...(etc) all negative events that may rise from it;
ENJOY;

Consider this code:

#include

int main(){
printf("I'm this cool right now: [%d]\n",0);
}

And the compiled binary:
student@csci4971:/tmp$ gcc -o a a.c
./a
student@csci4971:/tmp$ ./a
I'm this cool right now: [0]

Lets fire up gdb and patch that to something better than 0.

(gdb) disas main
Dump of assembler code for function main:
0x080483e4 : lea 0x4(%esp),%ecx
0x080483e8 : and $0xfffffff0,%esp
0x080483eb : pushl -0x4(%ecx)
0x080483ee : push %ebp
0x080483ef : mov %esp,%ebp
0x080483f1 : push %ecx
0x080483f2 : sub $0x14,%esp
0x080483f5 : movl $0x0,0x4(%esp)
0x080483fd : movl $0x80484e0,(%esp)
0x08048404 : call 0x804831c
0x08048409 : add $0x14,%esp
0x0804840c : pop %ecx
0x0804840d : pop %ebp
0x0804840e : lea -0x4(%ecx),%esp
0x08048411 : ret
End of assembler dump.
(gdb)

T
The printf call is here:

0x080483f5 : movl $0x0,0x4(%esp)
0x080483fd : movl $0x80484e0,(%esp)
0x08048404 : call 0x804831c

Two arguments are being pushed.
Argument 1 is:
(gdb) x/s 0x80484e0
0x80484e0: "I'm this cool right now: [%d]\n"

Argument 2 is: 0x0

The instruction that pushes the 0 is 8 bytes long (fd - f5)

0x80483f5 : 0xc7 0x44 0x24 0x04 0x00 0x00 0x00 0x00

The 0 happens to be the last 4 bytes. The exact format of your instruction
can be found in the instruction manuals. Or through trial and error, you'll
start to get a feel for it.

(gdb) break main
Breakpoint 1 at 0x80483f2
(gdb) r
Starting program: /tmp/a

Breakpoint 1, 0x080483f2 in main ()
(gdb) set *0x80483f9 = 0x00000539

Now the program is patched.

(gdb) disas main
Dump of assembler code for function main:
0x080483e4 : lea 0x4(%esp),%ecx
0x080483e8 : and $0xfffffff0,%esp
0x080483eb : pushl -0x4(%ecx)
0x080483ee : push %ebp
0x080483ef : mov %esp,%ebp
0x080483f1 : push %ecx
0x080483f2 : sub $0x14,%esp
0x080483f5 : movl $0x539,0x4(%esp)
0x080483fd : movl $0x80484e0,(%esp)
0x08048404 : call 0x804831c
0x08048409 : add $0x14,%esp
0x0804840c : pop %ecx
0x0804840d : pop %ebp
0x0804840e : lea -0x4(%ecx),%esp
0x08048411 : ret
End of assembler dump.
(gdb)


(gdb) c
Continuing.
I'm this cool right now: [1337]

Program exited with code 040.
(gdb)

well so that is it.
I already told you am on level 1 so don't expect me to do explanations for you. you should find out and lemme know instead of asking then what? (Sorry if you did not ask "Then what?" my bad) lol...

even if I don't get to level 10 (The Final stage) I would have known that I at least was in the competition... (My consolation)......

Google Translate in Ghana (Ga Chronicles)

Am currently at the Google translate workshop at Ashesi University in Labone Accra. its supposed to be a two day workshop day 1 covering Ga and day 2 Ewe both local languages of the Ghanaian Community.
So am in the first day of the workshop and its very interesting what am looking at here. I have always wanted to see how literate and illiterate people in terms of computing would behave when they have come together to do things. you know, like there is a project and the non technical people have ideas needed for the technical people to complete their tasks right. so here it is I have seen it and it did not take a travel over the seven seas. lol

Ok, to my experience; am seated here with another guy doing the translation thing, he is good at the language so we get a word or phrase together from the google translate console found here ggogle.com/transconsloe] and work it out. there is another man who has is supposed to be the language supervisor since he is good at the language. now here is the problem being faced here;
Words and phrases are being translated without the clear understanding and history of it and this is causing a lot of stir ups. its actually very simple. am not good in the Ga language though am a Ghanaian and just by working with this guy next to me I have realized a few interesting words and phrases some of which I never could have figured out in a million years if I was to meet it alive and kicking. :-D. The situation is this; words like windows, homepage, Home, etŋ all related to the web and computers and the suggestions being made are surprising. everyone seems to have a different definition of things related to ŋomputer which proves that they do not really understand computers and just as my first post on this particular blog stated; people know computers but don't understand it so such things become difficult. its very simple "Windows" as in the frames you see on your screen is defined as its name due to its relationship to the aŋtuall windows object. therefore translating such a word should maintain same meaning so in Ga it should be "Saŋflɛ" in this way its able to keep its shortness and direŋt meanings. Am a programmer and its very important when words are kept short, it makes the work easy and makes our users not skip "IMPORTANT INSTRUCTIONS"
My graŋe is that this very translation exercise does not end here. It goes all the way to whenever so I can always go back to change a few words. and then for the words that were not changed and were maintained with english names I ŋould also go baŋk to "independice" them like Dr. Nkrumah did to Ghana. :-D lol...

At the end of it all here is my official comment:
In next events such as this is, I suggest we get language speakers (professionals) who are related a bit to computers or something. else if we are gonna use people who aren't "Technically Inclined Enough" then we should adapt the idea of having the technical people give a clear definition of a phrase, word, sentence, blah blah blah before the language experts give a meaning to it. Take a word like "Spam" which simply means fat in meat (or similar) is used in place of junk mail. translating such a word we do not neccessarily need to translate it "AS Is" there has to be a bit of change in it so I will suggest a word familiar to "Something Unwanted" what we are looking for is short and meaningful words since am more of a fante speaker than ga I will give a suggestion for a Fante suggested word which is "Bozzi" meaning a "Fibroid" who wants a fibroid in their stomachs at anytime? well just as no one wants spams in their mail boxes.
Here is what we need to know, realize and understand; Google has given us the liberty to suggest what words to use in this database and for us to use in the end. we should use words that will make s happy not what will please them. its that simple. some of y'all after reading this may have your comments but thats what makes us different and I want to see how different you are so make it known down there as a comment. back to the case; we should define things as we want them not how others want them, we are setting the rules so lets set it right at the same time too. I love the Akan translation: Google Home -> Google Fiee, etc its just cool. lets do the same "Gas" lets do it! "Gambatte! Ja mata! (Japanese, now flexing my other language abilities) lol :D

Thursday, July 29, 2010

Enabling PHP-MySQL support (servers)

So I recently had to reinstall linux on my box after going through the death-seeking trauma of having to deal with sharing one hard drive with windows and linux. (Don't blame me for using windows, Certain programs need to be tested there. and I need to understand it in order to get my facts of it right). So I got done with installing my linux mint7 OS (Gloria) installed everything I needed. Then I tried to run scripts that require php and mysql to run better.
For some reason this wasn't working which meant PHP-MySQL support was not enabled and that meant enabling it. I went through the usual config, apt-get, synaptic package, bla blah blah and some how I still couldn't realize wat was up with it. I wasn't on the internet and google does very less for people offline.
I just kept searching for a way out and guess what it didn't work. lol
I connected to the internet to get help from the linux mint main site http://forums.linuxmint.com/viewtopic.php?f=47&t=19771&start=0 and there my solution was. very embarassing apache2 wasn't fully installed, that was quite a news to me cos I very well know that was the first thing I installed when sudo apt-get was initiated by myself.
anyways; I thought this was an experience to share so here it is. basically what am reflecting here is this; most of the time when we (computer programers and software engineers) get a situation *Shit Happens* we do a lot to solve it and sometimes just as life is we miss the little thing that could assist us. if you think this experience is damn then how come your codes come up with typos? lol
any way here is a list of solutions for you to try in case oneday you get into my situation;

This support is for Linux and Unix system users only with windows users its very simple you just need to install the mysqli.dll file and then do the configuration thing in the file. Windows users have a lot of already made packages and solutions so just keep google search and the solution will be there else leave a comment here and I will follow up ASAP.
for linux/Unix users, first you need to install of this

mysql-server
apache2,
php5,
php5-mysql
(These are just the versios I use now. it may differ depending on you preferences.)

if you are on linux interfaces the synaptic Package Manager should be a very good friend to your problem all you need to do is run it and type in these things in the search select them for installation and you are done else using the Terminal here is what you type sudo apt-get [name of package]. if you are on unix use this; sudo pkg_add -i -v [name of package] (this is for OpenBSD though, am not sure what its like on your Unix system)

This should work for you just as it has for me.

I have actually found a solution to Fixing a linux box with the grub problems and it seems to work out perfect when solving a lot of other problems, all you need is to understand the OS you are using and then put all files in the right directories, etc. I'll blog how I fixed my linux box after a forced shutdown and getting a grub problem recently. then I will try to explain as much as I can the structure of a linux OS possibly a UNIX system too well maybe windows too but that is if I don't get an invite to a court room. :D

Tuesday, June 8, 2010

Computer Programmers in Ghana

I have been with computers all my life, I could say since I was about six years. My dad got me and my brother our first computer system which run a win98 OS and was a compaq. ever since and even before that, my brother and I have really been into computers. Being able to use any type of technology has been one of our greatest aims and show offs lol.
growing up, I have come to the realization that computers have become a wide part of us in the world with almost everything running through computers and networks. My realization of computers in Ghana is that at least 90% of the population know what a computer is or can at least tell you a few wonderful things some of which microsoft and linux/UNIX companies including our dear apple is still thinking of how to come up with. but that is just by it. the real deal is they have an idea of it but out of the 90% who can define a computer system don't really know its real purposes. Just becuase you can clieck or press a button on a computer system does not make u a professional user. if that was the case we would have all been expects including the mad person carrying a spoilt keyboard walking around thinking he is the secretary general of the UN.

I was quite suprised when I met a lady at the recent google conference held in Accra AITI who said she programmed in C/C++ but was still in console. that was quite suprising for me cos most ladies would just stay in the class get a certification and end up the assistant to a manager who probably don't have a clue what a mouse is he just uses it "escuse me where is your mouse?", "I don't use one" thats like Oh My God. I hadn't seen her do anything but her being in the conference and having a notebook to follow the lectures gave me the hope of having at least a lady or two join our computing world.

Most guys would show off that they are computer programers, but don't have an idea how to compile hello world in C. the main cause of this all being that, people don't still know the value of custom applications and yes the whole computer system. I once a few times have heard people say computers are in their offices to prove they are a well established company or just to facebook.

my discoveries in this nation keeps getting excited every now and then. just recently I have discovered a new group, GTUGS; a computer related software development community right here in Ghana and its amazing the projects they are on. though they use a lot of Open Source I still think its cool cos they are able to at least understand and use them well