PHP
How to force http to https through .htaccess?
In order to force https:// in payment and other secure pages even if your customer try to access http:// or they forget. You can use .htaccess file and simple code.
Example –
I had created a page http://www.phpmind.com/blog/
I want my visitor to access https://www.phpmind.com/blog/
Here is code which will force them to redirect to https://
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
How to explode a string and create hyperlink?
Here is very easy code to create Tagged List with hyperlink.
I have found several events where we have to create dynamic URL with Tagged text.
I have used very simple approach. Just Exploded sting and replaced space with “-”
You can use PHP function preg_replace() or str_replace() then used here-docs for creating hyperlink.
Instead of Echo Hear Docs is much better solution.
And at last removed the last Comma, and return your comma separated Dynamic links.
$tags = "jQuery Tutorials, PHP Display Errors, Drupal Module"; $tagL=""; function b2b_article_LinkedTags($stringTags) { $expTags = explode(",",$stringTags); foreach($expTags as $key=>$Tagevalue) { $Tagevalue = trim($Tagevalue); $link = preg_replace('/[^-a-z0-9]/', '-', strtolower($Tagevalue) ); //$link = str_replace(" ","-", strtolower($Tagevalue)); $tagL.=<<<EOF <a href="./articles-tagged-$link">$Tagevalue</a>, EOF; } return substr($tagL, 0, -1); } echo b2b_article_LinkedTags($tags) |
How to test HTTPS CURL in development server?
HTTPS is secure HTTP communication based on SSL protocol (HTTP over SSL). Generally all sensitive info (like passwords, financial details, etc.) are sent over this transport. Common example: your gmail login is done through HTTPS channel and different payment gateway.
So here in this deal -
$postfields = array('field1'=>'value1', 'field2'=>'value2'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://foo.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); |
CURLOPT_SSL_VERIFYHOST is off. This allows you to test the CURL in your dev server without having HTTPS. PHP/Curl will handle the http request.
How to display XML output in Browser?
To display XML file on browser use this header.
<?php header("Content-type: text/xml"); ?> |
How to post XML using socket ?
I had discussed posting XML over HTTP using CURL in last post. Remember that was first method.
As I had promised on earlier post I would like to share second method with you. That is socket!!
Use this code and send your XML file.
<?php function postXMLToURL ($server, $path, $xmlDocument) { $contentLength = strlen($xmlDocument); $fp = fsockopen($server, 80, $errno, $errstr, 30); fputs($fp, "POST $path HTTP/1.0rn"); fputs($fp, "Host: $serverrn"); fputs($fp, "Content-Type: text/xmlrn"); fputs($fp, "Content-Length: $contentLengthrn"); fputs($fp, "Connection: closern"); fputs($fp, "rn"); // all headers sent fputs($fp, $xmlDocument); $result = ''; while (!feof($fp)) { $result .= fgets($fp, 128); } return $result; } function getBody ($httpResponse) { $lines = preg_split('/(rn|r|n)/', $httpResponse); $responseBody = ''; $lineCount = count($lines); for ($i = 0; $i < $lineCount; $i++) { if ($lines[$i] == '') { break; } } for ($j = $i + 1; $j < $lineCount; $j++) { $responseBody .= $lines[$j] . "n"; } return $responseBody; } $xmlpacket ='<AATHtlDispReq1> <Agency> <Iata>1234567890</Iata> <Agent>lgsoftwares</Agent> <Password>myapassword</Password> <Brand>phpmind.com</Brand> </Agency> <Passengers> <Adult AGE="" ID="1"></Adult> <Adult AGE="" ID="2"></Adult> </Passengers> <DestCode>OGG</DestCode> <CheckInDate>101009</CheckInDate> </AATHtlDispReq1>'; $result = postXMLtoURL("www.yourdomain.com", "/path/",$xmlpacket); $responseBody = getBody($result); echo $responseBody; ?> |
How to post XML using CURL?
Recently I was working in a hotel booking engine and found a couple of methods to post XML to server; I thought this might be good to share with my friends who want to post xml via HTTP POST method.
There are several ways to Send XML requests via HTTP POST.
I am going to show you two ways. Both are very simple and easy.
As first approach I have used a small xml file with CURL.
<?php $xml_data ='<AATAvailReq1>'. '<Agency>'. '<Iata>1234567890</Iata>'. '<Agent>lgsoftwares</Agent>'. '<Password>mypassword</Password>'. '<Brand>phpmind.com</Brand>'. '</Agency>'. '<Passengers>'. '<Adult AGE="" ID="1"></Adult>'. '<Adult AGE="" ID="2"></Adult>'. '</Passengers>'. '<HotelAvailReq1>'. '<DestCode>JHM</DestCode>'. '<HotelCode>OGGSHE</HotelCode>'. '<CheckInDate>101009</CheckInDate>'. '<CheckOutDate>101509</CheckOutDate>'. '<UseField>1</UseField>'. '</HotelAvailReq1>'. '</AATAvailReq1>'; $URL = "https://www.yourwebserver.com/path/"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_MUTE, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); ?> |
New PHP e-books
If you are looking for any other specific book of php and mysql contact me!
- Beginning PHP4
- Integrating PHP and XML
- Object Oriented Programming with PHP5
- PHP5 MySQL Programming for the Absolute Beginner
- Php Mysql Programming For The Absolute Beginner
- PHP MySQL Programming
How to Stop SQL Injection in MYSQL?
Every PHP-MYSQL programmer need to know Anti-SQL Injection.
Please take a look at very simple function which can save your database!!
<?Php function ClearInput($dirty){ if (get_magic_quotes_gpc()) { $clean = mysql_real_escape_string(stripslashes($dirty)); }else{ $clean = mysql_real_escape_string($dirty); } return $clean; } ?> |
How to Redirect with PHP?
You need to replace the URL above with the URL you wish to direct to.
Use this simple PHP script to redirect a user from the page they entered to a different web page.
<?php header( 'Location: http://www.phpmind.com.com/blog/' ) ; ?> |
How to display certain number of words from a record?
I got good php tutorial to display certain number of words from database; in place of Database record you can use any other regular string.
<?php $text = $row_recap['text']; if (strlen($text) > 300) { $ext = "... <a href='readmore.php'>read more</a>"; } else { $ext = ""; } function elliStr($s,$n) { for ( $x = 0; $x < strlen($s); $x++ ) { $o = ($n+$x >= strlen($s)? $s : ($s{$n+$x} == " "? substr($s,0,$n+$x) . "..." : "")); if ( $o!= "" ) { return $o; } } } echo (elliStr("$text", 300)) . $ext; ?> |

