Get data from XML containing namespaces or Colons in PHP

No Comments

Now every developer working with PHP knows how to extract values from XML using:

If the xml looks like this:

You can easily get the data using the following step:

But what if the XML looks like this:

I had a huge struggling with this a long time back. Though there are much complicated solutions are available. Like using new DOMDocument(). Which is of course a not easy to go with. Let me explain how I overcame this problem. The fix is of course not the way it is suggested but very effective and easy to work with. And since then, I have been using such xml with out problems. Here is what I did.

The out put will be

Now you have XML without the namespace and can easily work with your regular coding. Please submit your comments and let me know if this has worked for you? Thank you again for reading this.

Be Sociable, Share!

How to create a simple Membership Area in PHP

No Comments

Hello again, I am sure 90% of all the websites now a days offers Membership and in simple words the login area. The Membership PHP program totally depends upon your requirements. It can be highly complex to a very simple to use.

So, I am going to help you build a simple to use membership area. Your visitors can register and after your approval, they can visit your Membership area. To accomplish this we require:

1. MySQL database
2. Registration Page
3. Login Page
4. Your Membership folder/ page
5. Logout page

MySQL database

Lets start with step one. You need to have a database to store user login details. So lets see how the simple database would be

Database "user"
id email name password status
1 demo_amie@x-developer.com Amie amie004 1
2 demo_kathy@x-developer.com Kathy kat123 1
3 demo_kasper@x-developer.com Kasper kasi009 0

I assume you have a basic knowledge of SQL on how to create tables. Again, this is a demonstration of a simple to build Membership area. You can capture tons of information from the visitors while signing up like IP, date, phone no. etc. That is totally up to you.

Registration Page

Let go to step no. 2 creating a registration page. Let’s name the page as register.php. Find the code below:

So, the registration form is ready. Now, let’s write the php code for the registration form. I will explain the code in comments below:

And your registration page is done!

Login Page

Okay, now let us move forward to login page and membership area. Lets create a login form. And lets keep the file name as login.php

Now let’s write the code for this page. Remember, this is login page where you need to verify the login credentials. I have added a small feature for you. You can also tell the user if his account is not approved or disabled for violation of terms and conditions etc. Let’s see how we do it.

let us say your membership area is file member.php

Membership folder/ page

So, we are done with the 95% of coding so far. Now the user gets successfully logged in and enters your member’s area. Let’s do the coding for your member’s area now

Alright, now your membership pages are 100% secured. Only you can decide who can visit them. Not let us move on to our last step log out.

Logout Page

If the member is inactive on your website, he automatically gets logged out in an interval of mins. But that again depends upon your hosting company. Some hosting companies allows longer session which allows the inactive logged in user to stay on your website for longer interval. And many hosting companies do not allow this for security reasons.

But we can enhance the login period irrespective of the hosting company session killing time. We can do with with the help of cookies. I will explain this in another tutorial. For now, let’s understand the basic login.

So what left now is our logout page. Let’s name it as logout.php. The code for this page is simple and sweet.

I have used php validations to make sure that the form is submitted correctly. In general we also use java script validation which is nice and in real time. I will explain that in another tutorial.

I am sure by now, you have understood this tutorial. Please note, I made this tutorial for education purpose and it is very basic and simple. If you still want to use it on your website, be my guest!! :D

Again, thank you for reading and supporting me. Hope to get back soon with more lessons.

Be Sociable, Share!

Unzipping file online on linux server with PHP

No Comments

Everyone can easily unzip on a Windows or Linux provided OS access must be given. But how to unzip a file with PHP? This is very useful when you are uploading a heavy folder containing say more than 100 or may be 1000 files. Or if you are uploading a complete website to a new host.

If you upload the folder through FTP, FTP upload each files individually and run individual commands for each files and does not upload the complete folder in one go which takes a lot of time.

Say if you upload a folder of size 10 mb containing 400 files on a broadband 1Mbps connection, it will take you at least 20 mins to finish the upload. However, you will be amazed if you do it my way by zipping all the files, uploading it and unzipping them online, the process will take less than a minute.

The logic is simple to understand. When you zip the 400 files, the no. of file to upload now becomes one. So FTP can upload your file with just single command and the folder size is compressed to at least 50% of the original size. Make a file unzip.php. Let me explain the coding for this file:

You are ready to go. Just upload the unzip.php file to your directory where you have uploaded the zip folder. For example, your domain name is www.x-developer.com and the file you have uploaded is demp.zip. Your url to unzip the file will be:

http://www.x-developer.com/unzip.php?f=demo.zip

Please note:
This script requires php safe mode to be turned off. It will not work if the safe mode is turned on. If you do not know how to turn off the safe mode, let me explain:

open the php.ini file in the root. If you don’t find it, create a new file php.ini and add the following code:

safe_mode = off

Happy to help, if you have any questions.

Be Sociable, Share!

Send and receive data cross-domain through XML in PHP

No Comments

Okay so you are looking a simple way to transmit data from location to destination through XML. This is a very useful and popular method. Say you have two or more domains and you want to exchange data between them like storing data to your other domain’s database. This is not possible if you only use SQL query because you cannot access other domain’s database for security reasons. So let me explain how you do it. Let’s consider your 2 domains first:

www.domain1.com and www.domain2.com

And we want to send value from domain1.com to domain2.com. Let us have 2 files sendXML.php and receiveXML.php and upload them to their respective domains. Now the path for these 2 files becomes:

www.domain1.com/sendXML.php
www.domain2.com/receiveXML.php

Lets start with the coding for sendXML.php

So we were able to send the data to the destination. Now we need to get the data at the destination. Let’s do the coding for receiveXML.php now.

So now the system is complete. You have captured the value from XML and now you can do whatever you want to do with them. Store in the database, display on the page etc.

I have shown a very basic and simple to understand example. However, you can send values in bulk using this method if you know XML.

Be Sociable, Share!

Loading drop-downs with Ajax/ PHP and fetching values from database without refreshing the page

6 Comments

You must have come across this module many times usually when you have the primary drop-down for country and secondary drop-down for cities. Many are still using just Java Script function onChange(). This is a very old way of getting things done and also requires the page to reload when you select your primary drop-down in order to insert the values into the secondary drop-down.

Today I will show you how simple is it to load secondary drop-down using Java Script, Ajax and PHP. To start you will require a database. Let’s have 2 tables in our database namely
COUNTRY
CITY

COUNTRY id country  
10 UK  
 
CITY id city country_id
1 London 10
2 Bristol 10

Now, we have 2 dynamic drop-downs to create. Let us create the drop-downs first:

This will create the primary drop-down by fetching all the countries from database. In order to create the secondary drop-down, we need to write down some ajax code to define get_cities.

Now, we need to create a file cities.php, where we are going to send the country_id and get the list for cities in return.

Now, let me explain how this will work. We will add a Java Script function onChange() in the primary drop-down. When the primary drop-down is changed, the country_id is sent to cities.php.

In the file cities.php, using the country_id, we fetch for cities from the database and the value is return to the city drop-down. Pretty simple. Now your drop-down will work without refreshing the page. Good luck with it and let me know if you have any questions.

Be Sociable, Share!

SQL connecting multiple databases in a single query using INNER JOIN

4 Comments

Good morning!! Today I want to share how you can connect multiple databases using a single query. Many developers runs multiple queries in order to retrieve data from different databases for one single variable. This increases the load on server and the time to get result is slow. Why use multiple queries when the work can be done using a simple query?

For example you have 2 different databases:

database1
database2

and want to retrieve data from them. Let’s define these two databases.

Database1
Table A Column A1 Column A2 Column A3
Sam 23 100
Database 2
Table B Column B1 Column B2 Column B3
Monika 38 100

Now, you see we have defined the 2 databases. Lets try writing an SQL query to retrieve data. To retrieve the data from these 2 tables of 2 different database, you have to use INNER JOIN. Joining these w databases at column A3 and B3. Since we are joining these 2 databases, we want the query to know which database  to look in. So we will use Database1.TableA.ColumnA1 for retrieving data from columnA1 and similarly for others.

Let us write the query

Now, you will see the complete result in a single row. In case, the column names are same in both databases, you will see the column names same in the result and it will be impossible to get the value in PHP because 2 or more column name is same. To accomplish this, lets write another query

If you look closely, I have assigned a name of my choice to the column, now the result will have unique column names.

Now, you have the query, but how to connect the two databases using mysql_connect? The best way to do this is create a new user in your control panel and assign it to both the databases, now connect the both databases using mysql_connect.

 

I hope this helped you. Thank you for reading.

Be Sociable, Share!

Multiple SQL queries to multiple Databases in one PHP page

No Comments

Okay, so I dealt with this long time back where I wanted to get data from different databases in a single page. The first query seems to be executing fine. However, the 2nd query was returning MYSQL error: PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

The query seems to be fine and if I execute them seperately on PhpMyAdmin, they were getting results. I was confused for a while then I started looking closely and figured what the problem was.

See, in PHP, you need to connect to the database first before executing a query. This is how you do it

Without this, you cannot execute SQL queries using PHP. This is the first step. Now, we have connected the database using database name, username and password. And then if you run an sql query, your script knows from which database the data should be retrieved from.

But if you connect two or more databases simultaneously, the script doesn’t know from which database, it should retrieve the data. All you need to specify the database connection with each SQL query and now it knows which database it should connect to. Here is an example.

 

Now, the queries will execute with a charm. As you can see, we have clearly assosicated each SQL query with a Database connection so now it knows which Database to look in. Try it :-)

Be Sociable, Share!

Copy an image from one directory to another using PHP

No Comments

This is a very commonly used php function. You can do it by using the below function.

 

Let me explain you how you can do it. The code is defined below:

 

 

If you don’t know the path of your directories, here is the way to find out. Make a php file path.php and add the following code. Now, just upload this file to the directory whose path you want to find out and run it.

 

 

So that’s it. Pretty simple right? But let’s take it further and add some validation. If you add this code to your website, the script will keep copying the file every time this script is run. If you have too many images, this could slow down the server a bit.

So, the best way is to add a validation which will check if the image exists in the destination folder. It will copy the image from the source folder only if it is unable to find the image in the destination folder. You can do it bu using:

 

 

Here is the complete code for you

 

I hope this has solved your case! Best of luck :-)

Be Sociable, Share!

How to compress your website pages using PHP Gzip

1 Comment

You can compress your website using the function ob_gzhandler or ob_start(). ob_gzhandler sends the data after confirming the type of data is accepted by the client’s browser.

Just add this code at the top of your php page and the it compress your page and you will notice a big difference in the performance.

Be Sociable, Share!