Posts filed under ‘php code’
Simple PHP URL rewrite
June 29th, 2007
One thing I did for dmb is making dodosmb.com/profile/username work with my profile.php script. Normally without the help of .htaccess, you can use the value of the path info from the global variable
$_SERVER['PATH_INFO']
For example, create a php file named profile.php and in there put:
<?php echo $_SERVER['PATH_INFO']; ?>
Then upload the file and load it with yourServerPath/profile.php/dodo. The script will print out
/dodo
In my profile.php, I get the user by doing a simple sub string command like:
$user = substr($_SERVER['PATH_INFO'], 1); // this will get rid of the leading slash
This allows me to feed users to my profile.php by using the url dodosmb.com/profile.php/username
That’s still not pretty enough; it’s better if I can do dodosmb.com/profile/username. This is where .htaccess rewrite comes in. Here’s a great tutorial for .htaccess rewrite rules. To accomplish my goal, I used the following rewrite rules in my .htaccess:
RewriteEngine on
RewriteRule ^profile/([A-Za-z0-9]+)/$ /profile.php/$1
RewriteRule ^profile/([A-Za-z0-9]+)$ /profile.php/$1
Thoughts Aside
Some hosting service providers offer their services to the potential customers by having accessibility to the powerful server hosting. E-commerce hosting tools are very beneficial for the promotion of internet marketing in the most manipulated manner. The web host is accountable of fulfilling all the necessary requirements of the webmaster in accordance with the policies of the transaction. The powerful and latest servers are very helpful in restoring all kinds of backup files authentically. There is a need of modem for dial up connection which is really very easiest and cheapest way of internet accessibility.
A new way of array checking in PHP
January 10th, 2007
i just saw the funniest thing in PHP yet! below is taken from here.
This code is lifted from a socket handling class that I’ve been asked to work on today. Prepare to cringe:
if (eregi('Invalid argument supplied for foreach() in /usr/cvs/projects/13xx/1371/Classes/XML/XML_inc.php on line 392',$v[0])) { return false; }
It’s worth noting that the foreach() statement will move from line 392 with so much as a return key (as it did today?). It’s also worth noting that the path information is gloriously hard-coded. This was deemed dailywtf worthy in the office, and as such I present it to you now.
Happy Wednesday 
disable wordpress smiley for a certain post/page
February 2nd, 2006
okie, i thought someone has figured it out by now. but when i asked on the wordpress board, i got no response. so i digged around and figured a hack for it.
i noticed sometimes esp. on some pages, the smiley conversion does a poor job and converts the wrong text into smilies. it really mars my content. personally i want to be able to just turn the smiley off for a certain post or page.
the best way i can think of to do it is via a hack.
open your wp-includes/functions-formatting.php file, try find the function (possibly line 576):
function convert_smilies($text) {
then under it, replace:
global $wp_smiliessearch, $wp_smiliesreplace; $output = ''; if (get_settings('use_smilies')) {
with:
global $wp_smiliessearch, $wp_smiliesreplace, $post; $output = ''; $smileykey = get_post_meta($post->ID, "disable_smiley", TRUE); if (get_settings('use_smilies') && $smileykey != 'true') {
that’s all i needed to do.
now next time you write a new post/post, simply create a custom field with the key disable_smiley and value true to disable smiley conversion for just that post/page. isn’t that neat? 
dodosmail version 2.1
December 2nd, 2005
dodosmail has been upgraded to version 2.1 after securing it from header injection.
php alphabet output
July 6th, 2005
aw thanks to miki for the tip. i was wondering if there was a smarter way to print out the alphebat in php than just typing them out LOL
———-
Every letter of the English alphabet corresponds to an ASCII code, so rather than manually typing everything out, you can achieve the same result with a simple PHP loop.
The code below will output all uppercase letter from A to Z by running through ASCII codes 65-90 (the values for uppercase letters) and converting each code to its corresponding letter using the in-built PHP function chr().
<?php
for ($i=65; $i< =90; $i++) {
$x = chr($i); print $x;
}
?>
For lowercase letters, use the numbers 97-122 instead.
dodoupload 1.3
July 1st, 2005
worked most of today on releasing dodoupload version 1.3. if you never had problem with previous versions, you probably don’t need to upgrade. there is not much change from version 1.2. I rewrote the script mostly because some people have problems with http auth and i couldn’t figure out why. so now it’s cookie based login. i also got an email requesting to allow no login. so that’s an option now too.
amy from e-starr.com is now hosting regretless.com. i plan to reopen the registration for dodocounter
stay tune!
quickly fill an array
June 9th, 2005
i’ve been wondering what’s the quickest way to fill an array with a constant value? sometimes the code is necessary. for example, i have X number of items in an array and i wish to put them evenly into an table. to call another function, i need to pass in an array of values and an array of table width. well in my case, i just need to fill an array for table width with something like
$table_width = array('50%', '50%');
here’s my code to fill an array assuming $num_cols contains the number X.
$dummy = range(0, $num_cols - 1);
foreach($dummy as $val) {
$width_per[$val] = round(100/$num_cols)."%";
}
Is there a faster way to do this?
time left php function
May 5th, 2005
just a simple function that returns how many days, hours, minutes and seconds are left given a timestamp i put together. kinda bored so sharing some code XP
give either $time_left (time left in timestamp) or $endtime (an ending timestamp)
/* returns an array of [days],[hours],[minutes],[seconds] time left from now to timestamp given */
function timeleft($time_left=0, $endtime=null) {
if($endtime != null)
$time_left = $endtime - time();
if($time_left > 0) {
$days = floor($time_left / 86400);
$time_left = $time_left - $days * 86400;
$hours = floor($time_left / 3600);
$time_left = $time_left - $hours * 3600;
$minutes = floor($time_left / 60);
$seconds = $time_left - $minutes * 60;
} else {
return array(0, 0, 0, 0);
}
return array($days, $hours, $minutes, $seconds);
}
write better search engine in php
April 14th, 2005
my limited mysql knowledge always produced inaccurate search results when i write the board search engine. i want to do better this time for the new board so i’m reading this and trying out mysql regular expression and full text search. i’m pretty excited.
oh also, leslie reminded me. this perl style coding will save you from using back slashes and stuff like
<?=$var?>
PHP CODE:
i<?php
f(isset($j_sidebar)) {
echo <<<EOF
<div id="side">
{$j_sidebar}
</div>
<!--/side-->
EOF;
}
echo <<<EOF
<div id="main">
{$j_main}
</div><!--/main-->
</div><!--/outer-->
</body>
</html>
EOF;
?>
vs.
PHP CODE:
if(isset($j_sidebar)) {
echo "<div id=\"side\"> $j_sidebar </div><!--side-->";
}
echo "<div id=\"main\"> $j_main </div><!--/main--> </div><!--/outer--> </body> </html>";
i’ve been using the second for too long, it’s hard to change but it certainly doesn’t hurt to try something different
hehe another tip from leslie, you can insert multiple records into one table using just one query:
INSERT INTO blah (blah, blah) VALUES (blah, blah), (blah, blah), ....
mysql date column type
April 11th, 2005
i have to say that the decision to make the birthday column for members on the new board type DATE is a very good one. i never knew about all the magic mysql DATE type can do
but i still love the simplicity of time arithmetic unix timestamp provides.

