Ftp using curl
June 3, 2008 on 12:43 am | In Uncategorized | No CommentsThis post just a reminder for me, as every time I need it, I look through the manual for options, to send a file through FTP using the command curl
$ curl ftp://server/Path/To/Destenation -T FileToUpload -u username:password
I just keep forgetting it, hope it would help someone else ![]()
Good Old sscanf
February 28, 2008 on 6:06 am | In PHP | No CommentsWorking in PHP, all this time, and the big uses you find in regex, and all the string processing we need to, could make us forget our old friend that can easily solve a problem.
sscanf, do you remember it, well, for quite some time I didn’t, until I saw it in a random code I found online, so this post is a reminder that this function exists.
An example, if we have a date, lets say coming from a Mysql DB, (ofcourse you can use strtotime, but sometimes it has it’s limitations) and you want to get the year, month, day, hour, minutes, and seconds, and you have it in a string, you can parse it with some splits, but 1 call of sscanf can do the job.
$date = ‘2007-05-18 22:15:03′;
sscanf($date,‘%d-%d-%d %d:%d:%d’,$y,$m,$d,$h,$i,$s);
var_dump($y,$m,$d,$h,$i,$s);
and thats all.
PHP5 CAPTCHA
February 16, 2008 on 3:19 pm | In Free Software & Open Source, PHP, Releases | No CommentsI’ve written a small CAPTCHA library, in an OO style, and I love to share it with you
http://sourceforge.net/projects/php5captcha/
MySQL: Dynamic Select Queries
June 3, 2007 on 11:36 am | In MySQL | 4 CommentsAs we start to transfer our code to stored procedures, now we would have to start putting more logic in MySQL than before. In the past we would create an sql statement in our hosting language, and pass it by to mysql, put as we see the more improvement of performance and security in writing stored procedures, (also we employee people just experienced in MySQL to handle the sql Job) we now need a way to manipulate our sql statements with sql itself.
What I’m putting here is an example for a search procedure. imagine that you are looking for a certain profiles in the database, while specifing some data to be checked upon, and some data if missing won’t be included in our condition, lets say a user would check on gender and a range of age for our table like
CREATE TABLE Profiles (
id INT PRIMARY KEY,
gender CHAR(1),
age INT);
ok let’s create our search stored procedure
CREATE PROCEDURE SearchProfiles(
IN pGender CHAR(1),
IN pAgeStart INT,
IN pAgeEnd INT
BEGIN
SELECT id FROM Profiles
WHERE ((pGender IS NULL OR pGender = '') OR gender = pGender)
AND (pAgeStart IS NULL OR age >= pAgeStart)
AND (pAgeEnd IS NULL OR age <= pAgeEnd);
END
So now if you Null any of the inputs, it’s part of the query is removed, for example a NULL Gender would make the condition (pGender IS NULL OR pGender = ”) evaluates to true and it’s the right side of the total gender condition, which would let the gender = pGender Not Evaluated.
I guess you can improve the code more, by evaluating your stop conditions before you get in the select statement, and only calling a variable to test.
Hope it was useful ![]()
Butterfield and Fake, How they started Flicker
December 4, 2006 on 10:53 pm | In Internet | 2 CommentsThe original plan had been to create an online game. But they were just about out of money. And then Butterfield had this crazy vision of building a photo-sharing website, and before you knew it Flickr was a cultural phenomenon. Ya-hoooo!
How Much Do You Love Firefox
December 4, 2006 on 12:21 am | In Free Software & Open Source | No CommentsI Love /* */ and I hate #
December 3, 2006 on 2:02 am | In Uncategorized | 1 CommentWhat is wrong with multi-line comments? this is the question I’m asking myself right now, since I started programming I loved multi-line comments, (actually the comments you have to specify where do they end).
My first programming language was Pascal, they had comments like { my comments are here }, then C which I only knew about /* my comments are here */ and I never knew about
// my comments are here till the end of the line
until I learned C++, then Java, and PHP. They all have multi-line comments.
I’ve done work with Perl, and Bash before, and one of the reasons I hate to write Perl scripts was the
# comments till the end of the line
thing, yes I know that you can write multi-line comments as
# comment line 1
# comment line 2
but I just can’t stand it
Today, after hearing a lot about Ruby, (specially after the on Rails thing), well I thought, I should give it a try (given that you should learn a new language every year, and I didn’t do that this year). So I googled it, and damn, those # again, I hate them I really do, I can’t cope with them, I guess I have a psychological thing regarding these comments.
What am I wondering right now, what is really wrong with the multi-line comments? why new languages (except those which are C based) are abandoning multi-line comments, aren’t they cool anymore, or maybe there is some software engineering thing related to this issue.
WHAT IS WRONG WITH MULTI-LINE COMMENTS
Once Again I Failed To Fail
November 25, 2006 on 3:26 pm | In PHP | 1 CommentAfter my first failure to fail, now I hold the Zend PHP 5 Certification, it was a tough day day for me, it was like:
6:30
I started the second Mock-Up Exam, I passed it, but my results were worse than the first, “OK Ala’a, don’t panic, you might just be tired”, So I went took a bath a done some stuff.
9:30
My colleague, who’s supposed to take the exam with me, came online, and he wanted to talk the Mock-Up exam.
Me: but i don’t think that you have enough time left.
He: Do you think so.
Me: it’s 9:30 …
He: Yeah I noticed that.
Me: you have an hour and a half till the test.
Me: Unless you do it fast …
He: do u suggest to review the book! or take the test ??
Me: no take the test, that’s better.
10:05
I’m ready to leave, I closed the computer, and went to the door, I put my hands in my pocket, the keys are not there. “Damn, where did the keys go” I thought. I started looking around, I was starting to panic. “Don’t panic, you’ll find them” I thought.
I started searching, that lasted for 30 seconds, and then I panicked, I started throwing everything, and I found the keys, I don’t want to know how did they get there, I was glad I found them.
10:45
I reached the test center, everything went smoothly.
11:00
My colleague and I started the test.
11:40
I’ve answered everything, but I’m not sure about anything, I hate tests, now I have to click on the “End Exam” button. I clicked on it, and a confirmation message popped-up, I was so unsure, I started moving the message all over the screen, until I had the guts to press “OK”. And my longest period of time in this day was then.
“Processing ….”
“What if I failed”
“Processing …”
“I should’ve answered something else”
“Processing …”
“I should’ve postponed the test”
“Congratulations, you passed”
“Yes!!!!” I screamed.
“Congratulations …”, My colleague said.
“Thanks, Hope the best for you”, I said.
“Pray for me”, he said.
“I will”, I replied.
12:00
My Colleague gets out of the test room, …., He passed.
WoooooooooooooooW, I’m glad that everything went OK today.
Playing with PEAR Net_IMAP
November 22, 2006 on 12:54 am | In PHP | No Commentswhen it comes to IMAP programming, you should be very careful about IMAP commands you are issuing, an unneeded call can make your page much slower, that’s why usually IMAP Developers avoid programming with the php IMAP functions, yes they are written in C (c-client), but they don’t give you the commands you want exactly, so usually you would go for native IMAP (i.e. writing commands to a socket, and parsing the output).
Parsing is never an easy job, lot’s of cases and lot’s of bugs can appear, PEAR have a nice library called Net_IMAP, but of course, it doesn’t give you the freedom you need, so I use the IMAPProtocol class from it, as what I need is parsing only, I know the command, I just want to issue them, and to get them parsed.
for example to fetch the headers of a message
<?php
$imap->cmdConnect($mailhost,$mailport);
$imap->cmdLogin(”$username@$domainname” , $password);
$imap->cmdSelect($mailbox);
$msgHeader = $imap->cmdUidFetch($message_uid,’BODY[HEADER]‘);
$imap->cmdLogout();
?>
That looks fine, but Imagine this case, you have to sort the mailbox according to the From header, well of course you can do something like the code above, i.e. fetch the headers of all the messages, and parse for the From header, and sort accordingly, but the headers of messages are not a small part, you can have 25 lines for each message while you only need 1, which would slow down your page. if you have some experience with IMAP you would know that you code do this with a command like
A0001 FETCH 1:* (uid body.peek[header.fields (From)])
OK if you try this on IMAPProtocol, in the standard way, i.e $imap->cmdFetch(”1:*”,”(uid body.peek[header.fields (From)])”);
you would get lots of errors telling you that it’s not supported, so what should you do????
well here you have to do parsing yourself, but how can you do that, take a look at the code below:
<?php
$imap->_getCmdId();
$imap->_putCMD($cmdid,’FETCH 1:* (uid body.peek[header.fields (From)])’);
$nsarray=explode(”\n”,$imap->_getRawResponse( $cmdid ));
foreach ($nsarray as $line) {
if(strstr($line,”FETCH (UID “)) {
$UID_array[] = (int) (substr($line,strpos($line,”UID “) + 4));
}
if(strstr($line,”From: “)) {
$msgs_to=trim(strtolower(str_replace(”From: “,”",$line)));
$from_array[]=$msgs_to;
}
}
?>
Why would I do that, well My page is full of IMAPProtocol code, and I want to add the feature of sorting, I can’t just drop the whole page, and I want to make the best performance I can, in the less time I can.
Hope it was useful.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^




