WordPress is very easy to work on. PHP tutorial, how to integrate wordpress into php website. you can very easily integrate the wordpress with php or html site. Here we given code for same.
There may be only a few features of WordPress you want to use when integrating it with your site, or you may want your entire site run with WordPress. This tutorial will guide you through making your WordPress site look like your current design.
how to integrate wordpress into php website
How to start?
First you need to disable the wordpress theme. using following code.
open your header.php file from your wordpress theme and put following code in that file.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Create any php file and put following code in that file.
In the event you want to show ten posts sorted alphabetically in ascending order on your web page, you could do the following to grab the posted date, title and excerpt:
tutorial for PHP, domain search through Linux command and PHP. Searching the domain information through command line. You can search the domain using the Linux command.
domain search through Linux command and PHP
#whois google.com
using the following command you can find the all domain information.
If you want check when domain created then use following command.
#whois google.com | grep “Created | Creation”
If you want to create PHP script for domain search then use following code
If you want to find the old files from your system or server then you can use the following code. I used the following code for deleting the old files from system.
deleting all one day old files from folder through php
/*****************Get the path to Extension ****************/
$array_path = explode("/",$_SERVER['SCRIPT_FILENAME']);
<div id=":2e">$dynamic_path = "";
for ($i=0;$i
if($array_path[$i]!="")
$dynamic_path =$dynamic_path."/".$array_path[$i];
// This linux command will delete the one day older from specific folder.
exec("find ".$dynamic_path."* -mtime +1 -exec rm {} \;");</pre>
<div>
For deleting the old files from system I used the Linux command. You can execute the command using the exec function.
If you want to use the Linux command then use the following command
We given php code for Checking directory and deleting one day old files from folder using php, from folder in windows or linux through php language. For deleting the files from folder in any system (windows or linux). I created the PHP script for deleting the old files from system.
Earlier I created the script for deleting files from folder but that was for only linux. Then I created script which will work independently. Following script will work in mac or windows or linux.
deleting one day old files from folder using php
/***************** Delete one day old files ****************/
$dir = "C://Inetpub/vhosts/yourdomain/tmpfiles";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while ($file = readdir($dh)) {
if(!is_dir($dir.$file)) {
if (filemtime($dir.DIRECTORY_SEPARATOR.$file) < strtotime('-1 days')) { //if 1 days old, delete
<div id=":1o">echo "Deleting $dir.$file (old)\n";
unlink($dir.$file);
}
}
}
} else {
echo "ERROR. Could not open directory: $dir\n";
}
} else {
echo "ERROR. Specified path is not a directory: $dir\n";
}
closedir($dh);
// Above script is platform independent.</div>
In this tutorial we shown, how to add virtual host in wamp server on windows machine. You just need to follow our steps to setup virtual host in wamp server.
how to add virtual host in wamp server
Many PHP developers use the wamp server for development. For creating the virtual host just open the httpd.conf file from following location.
C:\wamp\bin\apache\Apache2.2.11
Then un-comment the following line
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Than open the httpd-vhosts.conf file from following location
Optimizing tables is very necessary for database. That will improve the your website performance. Optimize command is helpful to manage database files in right manner. This command will optimize the size of file size of database files.
How optimize all tables using php script
If you want to optimize the all tables of database then use following script.
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$dbconnect = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconnect) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected to database successfully';
mysql_select_db('YOUR_DATABASE',$dbconnect);
dbConnect();
$alltables = mysql_query("SHOW TABLES");
while ($table = mysql_fetch_assoc($alltables))
{
foreach ($table as $db => $tablename)
{
mysql_query("OPTIMIZE TABLE '".$tablename."'")
or die(mysql_error());
}
}
echo 'All tables optimized successfully';
mysql_close($dbconnect);
?>
The MySQL Optimize Table command will effectively de-fragment a mysql table and is very useful for tables which are frequently updated and/or deleted.
Many times php developers want to encode there php or important HTML,CSS or Javascript code. In this article I will show you the very simple encoding with using base64_encode method.
php encode and decode string with key
Here is working example.
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$str = 'This is an encoded string';
echo base64_encode($str);
echo base64_decode("VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==");
?></pre>
<pre>
you can create your own antilogarithm for encoding. For more information you can check the following URL
If you want to use the mongodb with php then you need to install pecl libraries. Mongo extension is not bundled with PHP.
The MongoDB server is built to already work with your current web server. The problem is that you’ll need to install drivers for your preferred backend language – PHP, Ruby, Node.js, Perl, whatever.
How to use mongodb with php
For installing the Mongo use following URL:
If you dont have pecl installed on your linux machine then use following command.
# yum install php-pecl*
then use following command
# pecl install mongo
Then open the php.ini file. if you are using the linux then use following command.
# vim /etc/php.ini
## Add following lines, end of php.ini file ##
# MongoDB Driver
extension=mongo.so
Restart the apache webserver using following command
# /etc/init.d/httpd restart
If Mongodb server is running then use can test your mongodb database using following code:
Create the mongotest.php file
<!--?php
// connect
$m = new Mongo();
// select a database
$db = $m--->wordpress;
$collection = $db->wordpressapi;
// add an element
$obj = array( "title" => "Sony this Good.\n", "author" => "Wordpressapi.com" );
$collection->insert($obj);
// add another element, with a different "shape"
$obj = array( "title" => "Wordpressapi.com", "online" => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
echo $obj["author"] . "\n";
}
// disconnect
$m->close();
?>
If you open this file in browser then you can see the following words in browser:
Sony this Good. WordPressapi.com Sony this Good. WordPressapi.com
If you want to use the mongodb with php then you need to install pecl libraries. Mongo extension is not bundled with PHP.
For installing the Mongo use following URL:
If you dont have pecl installed on your linux machine then use following command.# yum install php-pecl*
then use following command# pecl install mongo
Then open the php.ini file. if you are using the linux then use following command.
# vim /etc/php.ini
## Add following lines, end of php.ini file ### MongoDB Driverextension=mongo.so
Restart the apache webserver using following command
# /etc/init.d/httpd restart
If Mongodb server is running then use can test your mongodb database using following code:
Create the mongotest.php file
<!--?php
// connect$m = new Mongo();
// select a database$db = $m--->wordpress;$collection = $db->wordpressapi;
// add an element$obj = array( "title" => "Sony this Good.\n", "author" => "Wordpressapi.com" );$collection->insert($obj);
// add another element, with a different "shape"$obj = array( "title" => "Wordpressapi.com", "online" => true );$collection->insert($obj);
// find everything in the collection$cursor = $collection->find();
// iterate through the resultsforeach ($cursor as $obj) { echo $obj["title"] . "\n"; echo $obj["author"] . "\n";}
// disconnect$m->close();
?>
If you open this file in browser then you can see the following words in browser:Sony this Good. WordPressapi.com Sony this Good. WordPressapi.com
Mongodb is used for many projects now, it is quite fast. I given full detailed information about How to install Mongodb on linux box. Before installing the Mongodb on linux box you need to install following packages on box.
[root@sonyk-pc mongodb-linux-i686-1.6.2]# ./bin/mongod
./bin/mongod --help for help and startup options
Thu Sep 9 13:10:55 MongoDB starting : pid=22159 port=27017 dbpath=/data/db/ 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Sep 9 13:10:55 db version v1.6.2, pdfile version 4.5
If you want to create mongodb service in linux then use following steps:
create the File: /opt/bin/mongodb-stop
Put following code in that file:
#!/bin/bash
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
if [ "${pid}" != "" ]; then
kill -2 ${pid};
fi
create the File: /opt/bin/mongodb-start
Put following code in that file:
#!/bin/sh
/opt/mongodb/bin/mongod --config /opt/config/mongodb \
## --upgrade \ ##runs a database upgrade option if needed \
File: /opt/config/mongodb
Put following code in that file:
# Configuration Options for MongoDB
#
# For More Information, Consider:
# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters
# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration
dbpath = /srv/db/mongodb
logpath = /srv/db/mongodb.log
logappend = true
bind_ip = 127.0.0.1
port = 27017
fork = true
auth = true
# noauth = true
Do that file as linux executable
chmod +x /opt/bin/mongodb-start
chmod +x /opt/bin/mongodb-stop
We’ve also created a very basic “init script” as a wrapper around the mongodb-start and mongo-stop scripts described above. You will still need to modify and manage the configuration of your MongoDB server in the files above. This script only provides a means for ensuring that MongoDB will start at boot. Issue the following commands:
wget http://library.linode.com/databases/mongodb/reference/init-rpm.sh
mv init-rpm.sh /etc/rc.d/init.d/mongodb
chmod +x /etc/rc.d/init.d/mongodb /etc/init.d/mongodb
chkconfig –add mongodb
chkconfig –level 35 mongodb on
You will also need to create a user and group for mongodb; issue the following command:
useradd -M -r --home-dir /opt/mongodb mongodb
Now issue the following command to ensure that the MongoDB user you just created will have access to all required files in the /srv/db/ hierarchy:
chown mongodb:mongodb -R /srv/db/
To start and stop MongoDB using the init script, issue the appropriate command from the following:
/etc/init.d/mongodb start
/etc/init.d/mongodb stop
For checking the web admin interface of Mongodb – listening on port 28017
Check this URL : http://localhost:28017/
Before installing the Mongodb on linux box you need to install following packages on box.[root@sonyk-pc Download]# sudo yum -y install git tcsh scons gcc-c++ glibc-devel[root@sonyk-pc Download]# sudo yum -y install boost-devel pcre-devel js-devel readline-devel[root@sonyk-pc Download]# sudo yum -y install boost-devel-static readline-static ncurses-static
For 32bit user use following command[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz
For 64bit use following command[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.2.tgz
[root@sonyk-pc Download]# tar xzf mongodb-linux-i686-1.6.2.tgz [root@sonyk-pc Download]# cd mongodb-linux-i686-1.6.2
Mongo stores database in data/db folder. so we need to create those folder using following command.
[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo mkdir -p /data/db/[root@sonyk-pc mongodb-linux-i686-1.6.2]# sudo chown `id -u` /data/db
Using following command you can start the mongo database.
[root@sonyk-pc mongodb-linux-i686-1.6.2]# ./bin/mongod./bin/mongod –help for help and startup optionsThu Sep 9 13:10:55 MongoDB starting : pid=22159 port=27017 dbpath=/data/db/ 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Sep 9 13:10:55 db version v1.6.2, pdfile version 4.5
If you want to create mongodb service in linux then use following steps:
[root@sonyk-pc Download]# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz [root@sonyk-pc Download]# tar xzf http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.2.tgz[root@sonyk-pc Download]# mv mongodb-linux-i686-1.6.2 /opt/mongodb[root@sonyk-pc Download]# mkdir -p /srv/db/mongodb[root@sonyk-pc Download]# touch /srv/db/mongodb.log
[root@sonyk-pc Download]# mkdir /opt/bin/[root@sonyk-pc Download]# mkdir /opt/config/
create the File: /opt/bin/mongodb-stopPut following code in that file;
create the File: /opt/bin/mongodb-startPut following code in that file:
#!/bin/sh
/opt/mongodb/bin/mongod --config /opt/config/mongodb \## --upgrade \ ##runs a database upgrade option if needed \
File: /opt/config/mongodbPut following code in that file:
# Configuration Options for MongoDB## For More Information, Consider:# - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters# - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration
dbpath = /srv/db/mongodblogpath = /srv/db/mongodb.loglogappend = true
bind_ip = 127.0.0.1port = 27017fork = true
auth = true# noauth = true
Do that file as linux executatblechmod +x /opt/bin/mongodb-startchmod +x /opt/bin/mongodb-stop
We’ve also created a very basic “init script” as a wrapper around the mongodb-start and mongo-stop scripts described above. You will still need to modify and manage the configuration of your MongoDB server in the files above. This script only provides a means for ensuring that MongoDB will start at boot. Issue the following commands:
wget http://library.linode.com/databases/mongodb/reference/init-rpm.shmv init-rpm.sh /etc/rc.d/init.d/mongodbchmod +x /etc/rc.d/init.d/mongodb /etc/init.d/mongodbchkconfig –add mongodbchkconfig –level 35 mongodb on
You will also need to create a user and group for mongodb; issue the following command:
useradd -M -r --home-dir /opt/mongodb mongodb
Now issue the following command to ensure that the MongoDB user you just created will have access to all required files in the /srv/db/ hierarchy:
chown mongodb:mongodb -R /srv/db/
To start and stop MongoDB using the init script, issue the appropriate command from the following:
/etc/init.d/mongodb start/etc/init.d/mongodb stop
For checking the web admin interface of Mongodb – listening on port 28017Check this URL : http://localhost:28017/
PHP tutorial for, Post message to another server through fopen and fsockopen in php. When this comes to sending data to server to server we need to post the data using web services.
Many people want to use the curl php method but there are serious issues with that.
Post message to another server through fopen and fsockopen in php
So I recommended to use fopen function to post the message to server to server.
You can use the following code for post the message to another server using fopen and fsockopen
function post($host, $post_url,$port,$data) {
$errno = 0;
$errstr = '';
$path = str_replace($host,'',$post_url); // IP of minor instance
$header_variables = "POST / HTTP/1.1\r\n";
$header_variables .= "Host: $host\r\n";
$header_variables .= "Connection: Close\r\n";
$header_variables .= "Content-Type: application/xml\r\n";
$header_variables .= "Content-Length: " . strlen($data) . "\r\n\r\n";
// establish the connection and send the request
$fp = fsockopen($host, $port, &$errno, &$errstr, 15);
if ($fp) {
stream_set_timeout($fp, 15);
fputs($fp, $header_variables);
fputs ($fp, $data); //data written
fputs ($fp, "\r\n"); //request posted
$response = stream_get_contents($fp);
$info = stream_get_meta_data($fp);
if (!$info['timed_out'] && $fp) {
//get response and everything is ok
} else {
trigger_error('Timed out for '.$post_url);
exit();
}
fclose($fp); //close the file
} else {
trigger_error('Failed to open socket connection. ');
exit();
}
}
using CURL function post message also possible for that you can use following code
<!--?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' =--> 'Foo', 'file' => '@/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>