Using htpasswd with shell_exec in PHP

I tried to create a PHP script which will update my password file, using the Unix command “htpasswd”:

$out = shell_exec(“htpasswd -b myfile TEST AA”);
echo “<pre>$out</pre>”;

Problem was.. I never get any messages in the $out. This only come from the htpasswd, which only output on the STDERR. To solve that, just redirect the STDERR on the STDOUT with 2>&1:

$out = shell_exec(“htpasswd -b myfile TEST AA 2>&1“);
echo “<pre>$out</pre>”;

Reset your MySQL root password

I had a MySQL server, and.. I forgot the root password. Inside have some “important” datas, so I found a solution to reset your root password and get back your datas.

First, we should stop the MySQL server:

/etc/init.d/mysql stop

Then start the server with special options (Don’t forget the “&” at the end, to start the server in background):

/usr/bin/mysqld_safe –skip-grant-tables –skip-networking &

Connect to the server in client:

mysql mysql

Now, the most important, change the root password:

UPDATE user SET password=PASSWORD(‘Mysuperpassword’) WHERE User=”root” AND Host=”localhost”;

We just need to close the connection, close the server. and relaunch the server in “normal mode”:

mysqladmin shutdown
/etc/init.d/mysql start

And.. it’s fine, you can use again your root user, with the new password :-)

MySQL

MySQL

Be smarter everytime you open your browser..

Because most of people don’t have general culture.. this is a little something, can help you to be smarter everytime you open your browser. Just change your homepage to the random article page of wikipedia..

French: http://fr.wikipedia.org/wiki/Sp%C3%A9cial:Page_au_hasard
English: http://en.wikipedia.org/wiki/Special:Random

Wikipedia

Wikipedia

How to hide your “facebook relation status” to your girlfriend(s)

Ok, so first, I want to decline all the responsability if you lose your girlfriend after that.. I just want teach you a technical point of Facebook.

As you know, we can set a relationship status on facebook, “single“, “in relationship“, “complicated“,.. That’s really interesting if you have only one girlfriend, but if you want to have a second one, it’s possible, and transparent ! We will use the privacy settings, which is very granular.

On the home page, just go to the Account, then “Privacy settings“. On the setting, let’s go to the “Custom settings” section. Just search for the “Relationships” line.. and use the custom settings.

Custom privacy settings

Custom privacy settings

Then.. you can “blacklist” someone. this person won’t be able to see your relation status ! You can check with the preview profile and select your girlfriend.. then you’ll see what she can see.

This short tutorial also can be use for other purpose, like hide your status or “parties pictures” from your familly.

Monitor ping and packet lose with MRTG

The ping and packet lose can be a good indicator for your server, so we will add it to MRTG, to have some very new graphs. First of all, we will create the bash script file. This script will parse the output of ping. You can change the destination of the ping with the ADDR variable. Then just save it (don’t forgot to make it executable with “chmod +x nameoffile“):

#!/bin/sh
ADDR="61.148.60.42"
PING="/bin/ping"
DATA=`$PING -c10 -s500 $ADDR -q `
LOSS=`echo $DATA | awk '{print $18 }' | tr -d %`
echo $LOSS
if [ $LOSS = 100 ];
then echo 0
else
echo $DATA | awk -F/ '{print $5 }'
fi

Second point, we should add the graph to our MRTG configuration file (/etc/mrtg.conf). You can eventualy change the “MaxBytes” option and the “XSize/YSize” to change the size of the graph:

Title[localhost.ping]: RTT to 61.148.60.42 (Beijing, China)
PageTop[localhost.ping]: Round Trip Time

Target[localhost.ping]: `/home/mycado/mrtg_ping.sh`
MaxBytes[localhost.ping]: 2000
Options[localhost.ping]: growright,unknaszero,nopercent,gauge
ShortLegend[localhost.ping]:
LegendI[localhost.ping]: Pkt lose %
LegendO[localhost.ping]: RTT (avg)
YLegend[localhost.ping]: RTT (ms)
XSize[localhost.ping]: 600
YSize[localhost.ping]: 150

Then we just need to recreate the index.html with indexmaker and reload MRTG with:

r34xx:/# indexmaker /etc/mrtg.cfg --columns=1 --output /var/www/mrtg/index.html
r34xx:/#  mrtg

If you have some warning, ignore it, and start MRTG again, after 2 or 3 times it should be fine.

RTT with MRTG

RTT with MRTG

Basic use of crontab

Crontab is a job scheduler based on time, you can for example launch a bash script every 2 hours or everyday at the same hour. We will just need to write one command like “* 20 * * * /home/moi/myScript”.

The first fifth arguments specify when cron should execute the script, and the 6th is the command itself:

  • 1st argument: Minutes
  • 2nd argument: Hours
  • 3rd argument: Day of the month
  • 4th argument: Month
  • 5th argument: Day of the week

For example, this command will be executed every 5 minutes:

5 * * * * /home/test/myscript

Also, this script will be executed every 1st day of the month:

* * 1 * * /home/test/myscript

To add this job to our crontab, it’s easy, just need to use the editor, with the -e argument:

crontab -e

If you want to list your crontab you can use the -l argument, and the -r argument to remove one:

crontab -l
crontab -r

By default, every cron jobs sends an email to the user account. If you don’t want, you just should add the following command at the end of the cron job:

>/dev/null 2>&1

Don’t forget to sometime check your crontab list, don’t keep some useless job ;)

Avoid error when inserting data in MySQL with PHP

If you need to insert data from the “outside”, like from an user or from a file, you can have problems with some special characters. The basic example is when an user add a comment to your page. These characters are \x00, \n, \r, \, , and \x1a.

We can have some security issues (SQL injection):

<?php
$user = "admin";
$pass = "' OR ''='";

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
mysql_query($query);
?>

So the query will be:

SELECT * FROM users WHERE user='admin' AND pass='' OR ''=''

To avoid that, we have a function named mysql_real_escape_string, which will add a “\” before these special characters:

$user = mysql_real_escape_string("admin");
$pass = mysql_real_escape_string("' OR ''='");

The query will be:

SELECT * FROM users WHERE user='admin' AND pass='\' OR \'\'=\''

And when you need to use these data, just use the stripslashes to un-quote the text:

$var = stripslashes("mum\'s home");

Simple.. ;-)

Next Page »