Posts Under php Category
Remove all linebreaks in PHP
If you are having problems with a string that keeps adding a line break when output from PHP to HTML then the following code will work wonders for you! $string_with_line_break = "blabla\nbla\r"; $string_without_line_break = trim(preg_replace( "/\s+/","",$string_with_line_break));
Replace all spaces in HTML with except in HTML tags using PHP
If you would like to replace all the spaces in an HTML string with so that you can render it out to the browser but you also want to retain current spaces in HTML tags you can use the below method: $inputString = ‘<span class="color:red">1</span> 2 3′; $outputString = preg_replace_callback("#>[^<]+<#","relSpaces",$inputString); function relSpaces($match){return str_replace(" "," ",$match[0]);} This will replace: “<span>1</span> 2 3″ ..to look like this: “<span>1</span> 2 3″
Using PHP to validate an IP address
You can use the PHP code below to check if an IP address is valid or not. $ip = ""; //enter a valid or invalid ip address here if(filter_var($ip, FILTER_VALIDATE_IP)) { // The IP Address is valid } else { // The IP Address is not valid } Wasn’t that easy!?!
Hide all error messages PHP
PHP Error messages showing up in your web applications are a dangerous thing. Not only does it look unprofessional, but it is also a serious security concern! Once you have completed debugging your website or web application you can place the following one liner at the beginning of your code, this will turn off error reporting and therefore make sure that no application details are spilled to your users. error_reporting(0);…
What is xmlrpc.php?
It is a script which allows clients to make procedural calls over the net. As it says in the name, the encoding is XML and because it is used on websites we can make the fair assessment that it uses the HTTP protocol. If we break the name down we get: XML transmition via Remote Procedure Calls. So you are still not sure what this is all about? Read on…..
Stop That Referrer in PHP!
Today I will show you a method to stop the traffic that a referrer site is sending your way, you may wonder why you would want to ignore traffic, after all, isn’t inbound traffic to your site fantastic no matter what? The simple answer is “NO!” Let me explain it in a real life situation so that you can better understand where to use this. Understanding where to use it…
Error 500 php_value .htaccess
I noticed on some server setups I could not do the following inside a .htacess file: php_value upload_max_filesize 250M php_value post_max_size 250M I was getting an error 500 Internal Server Error with the above code in the .htaccess file. What to do to correct this is create a file called php.ini and save it in the same location as your .htaccess file and enter the following code instead: upload_max_filesize =…
CSS background image data: base64?
What the heck is all that Gobbledygook in the CSS? So you have noticed that bit of css that says something like this in the CSS source-code and you have no idea what it could be? url(“data:image/gif;base64,R0lGODlfMAMEAIABAKysrObm5iH5BAEAAAEALAAAAAAwAwQAAAJOhI+py30Bo5y02ouz3rz7D4biSJbmiabqyrbuC8fyTKPOjedHzff+DwwKh8Si8YhMKku6pvOxjEqn1Kr1is1qt7ynV8cNi8fksvmMTiMLAD4=”) no-repeat scroll 50% 0 transparent It is a technique called Data URLs and using PHP – or your favourite server-side script – you can generate these nifty little things. An example of how…
PHP's typeof – gettype()
Quite often you may need the ability to check what type of data type a variable is. In most programming languages this is possible to do and is usually called typeof(). PHP too has a way to check this, it is called gettype() and it’s so simple to use. All you have to do is echo out a call to gettype() with your variable as the first argument. Like so:…
Actionscript 2 & PHP Data Transfer
In the below example we will use Actionscript 2 to call a remote PHP file that will return data to it. It is a simple way of transferring data between the server and client. Actionscript 2 Code: callRemoteFile = function(ourVariable:String) { var result_lv:LoadVars = new LoadVars(); result_lv.onLoad = function(success:Boolean) { if (success) { …
Unique Random Numbers in PHP
If you would like to show random numbers using PHP you can do this: <?php $min = 1; $max = 100; $total = 100; $arrItems = array(); while ( count($arrItems) < $total ) { $item = mt_rand($min,$max); …
HTML mail() Sending as Plain Text
$to = "email@address.com"; $subject = "SUBJECT"; $message = "<b>MESSAGE</b>"; $headers = ‘MIME-Version: 1.0′ . "\r\n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "\r\n"; $headers .= ‘From: Mailer ‘ . "\r\n"; mail($to, $subject, $message, $headers); The above code does not always send emails in HTML as it should, it turns out there is a PHP Bug (http://bugs.php.net/15841) that comes into play with QMAIL and carriage returns. If this is the case you…