A Web Design Blog



Sunday, April 27, 2014

Ever wondered how those apps that you use in facebook are able to post content to your profile or your groups ? Well here is the solution. In my last post I've dealt with how to Login to your facebook account using the PHP SDK. And in this post I'll be dealing with how to post to facebook using PHP SDK. Watch the video below to know how to do


Useful Post : Login and Get name of the user using the Facebook PHP SDK

Video Tutorial :



 Download Source 

Wednesday, April 23, 2014

Hello there. I recently created a facebook web app that lets you to post to multiple groups, pages and your profile with just one click. You can post a message and even a link. Sounds cool right ? You can find the app here. And so I surfaced a lot through the facebook php sdk while creating the app. So now I thought of writing a series of posts dealing with the facebook php sdk.

In this post I'll be dealing with how to login to a facebook account using the facebook php sdk and how to retrieve the user information. And so lets get started.


VIDEO TUTORIAL



Prerequisites :


You need to have the following :

  • Fair knowledge on PHP

  • Facebook PHP SDK (We will be downloading this)

  • Your Text editor

Creating the app :


The first step is to create the actual facebook app. And this will be a kind of a communication link between facebook and your site. Follow the steps below to create your app :

1. Go to Facebook Developers Page.

2. Click on apps on the header and go to Register as a Developer. 

3. And once you have registered as a developer you will get into the documentation. (I swear Facebook has a really bad documentation)

4. Click on the same apps tab and click on Create a new app.



5. Enter a name, choose a category and proceed with creating the app.

6. In your app dashboard go to the settings tab and enter a contact email address.

7. Click on Add Platform and select Website.

8. In the site URL text box type in the domain of your site if you have one else type in localhost if you would be testing the app at your localhost. (Note : Facebook PHP SDK code would work only in the site which you mention here)

9. Save it return to your dashboard.

10. Make a note of your App ID and App Secret.

Bringing in the Facebook Login :


And now that you have completed creating the app and it is time to creating the login functionality. Follow the steps below to do so.

1. Download the Facebook PHP SDK.

2. Unzip it and copy the src folder inside it to the root of your app.

3. Create an index.php file where you placed the src folder.

4. Fire up your index.php file and type in the following code.
<?php

require_once 'src/facebook.php';

$fb = new Facebook(array(

'appId' => 'YOUR APP ID',

'secret' => 'YOUR APP SECRET ID'

));

Remember to replace your app id and your app secret. What this basically does is it creates an instance of the class BaseFacebook present in the facebook.php file.

5. And now to login the user add the following code.
if($fb->getUser() == 0){    //checks if the user is already logged in

$loginUrl = $fb->getLoginUrl();   //carries out if user not logged in

echo "<a href = '$loginUrl'>Login</a>";

}

else{

echo "<a href = 'logout.php'>Logout</a>";  //we will be creating the logout.php file later

}

In this piece of code we are letting the user to login to your site using his facebook account. We are displaying the login link if the user is not logged in else we are displaying the logout url.

6. We have completed logging in the user and now the job is to display the profile picture and name of the user. To do add the following code in the else part.
$api = $fb->api('me');

$name = $api[first_name] . " " . $api[last_name];

$id = $api[id];

echo "<img src = 'http://graph.facebook.com/$id/picture?width=150&height=150'><br>";

echo "Hi $name";

7. And so the only job left is to create the logout.php file. Create a file named logout.php and add the following code in it.
<?php

require_once 'src/facebook.php';

$fb = new Facebook(array(

'appId' => 'YOUR APP ID',

'secret' => 'YOUR APP SECRET ID'

));

$fb->destroySession();

?>

Replace your app id and the app secret in the respective places.

 Download Source Code

And that's it ! We have successfully created a facebook which lets the user to login and we have displayed the user's profile picture and his name ! We are done for the tutorial. Stay tuned for more tutorials on Facebook PHP SDK. Happy Coding :D

Monday, April 21, 2014

Hello. In this post we will discussing on how to create a contact form in php for your website with core php and this would mean that we would be coding our own contact form and not use any other client for that. And using this method you would be easily able to track the number of people who submitted your contact without having to use Google Analytics. And a point to note is that this form would display error messages in a good looking way.


Creating The Contact Form :


For creating the contact form we would be using PHP for the server side part and we would be using twitter bootstrap for the design. And so the first part in creating the contact form is the markup and the design. The following will be the output of our markup code given below. You can edit the code to your needs.


CODE :


<!DOCTYPE HTML>

<link rel = "stylesheet" href = "libs/bootstrap.css">
<link href='http://fonts.googleapis.com/css?family=Monda' rel='stylesheet' type='text/css'>

<body style = "background-color: #4ea6e7;">

<center>
<div style = "background-color: #fff; border-radius: 20px; position: absolute; top: 10%; padding: 20px; width: 36%; left: 32%;">
<center><h2 style = "font-family: Monda;">CONTACT US !</h2></center>
<hr>
<?php
if(!empty($_GLOBALS['error'])){
echo "<div class = 'alert alert-danger'> " . $_GLOBALS['error'] . "</div>";
}
if(!empty($_GLOBALS['done'])){
echo "<div class = 'alert alert-success'> " . $_GLOBALS['done'] . "</div>";
}
?>
<form action = "" method = "post" class = "form-group">
<input type = "text" class = "form-control" name = "name" placeholder = "Name" style = "width: 100%;"><br>
<input type = "email" class = "form-control" name = "mail" placeholder = "Email" style = "width: 100%;"><br>
<textarea class = "form-control" name = "message" placeholder = "Message" style = "width: 100%; height: 150px; resize: none;"></textarea><br>
<input type = "submit" value = "Send Mail" class = "btn btn-primary">
</form>

</div>
</center>

</body>


NOTE : DO NOT CHANGE THE NAME ATTRIBUTE IN INPUT FIELDS ELSE YOU WOULD HAVE TO CHANGE THAT IN PHP CODE TOO

And now the markup is completed and the rest is to make it working with PHP. Make sure that if you are running this in your localhost then you need to configure your SMTP else if you are running it on a online server then it would mostly be pre-configured. Add the following code above the <!DOCTYPE HTML> in the markup code.

PHP CODE :


<?php

$_GLOBALS['error'] = "";

if(!empty($_POST)){
$name = $_POST['name'];
$email = $_POST['mail'];
$message = $_POST['message'];

if(empty($name) || empty($email) || empty($message)){
$_GLOBALS['error'] = "<b>Oops !</b> All Fields are Mandatory !";
}
else{
$_GLOBALS['error'] = "";
$message = $name . "<br><br>" . $message;
mail("YOUR MAIL ID","Contact Form",$message,"Content-type:text/html;charset=UTF-8\r\nFrom: $email\n");
$_GLOBALS['done'] = "<b>Success !</b> We'll get in touch with you soon !";
}

}

?>

NOTE : Replace YOUR MAIL ID with the mail id where the user's mail has to be sent.

 Download Source Code


And that's it about creating the contact form ! Feel free to post your doubts below. Happy Coding :)

Sunday, April 20, 2014

Session hijacking would sound like something too big for newbies to php. Well here in this post I will be dealing with what is session hijacking and how to prevent it.


YOU MIGHT ALSO LIKE : What is and How To Prevent XSS Scripting in PHP - PHP Security

What is Session Hijacking ?

Session hijacking is a kind of a hacking method that an hacker can use to get access to other accounts provided that you are having a login system and you use sessions for that. Consider an example where you are using a session variable to store the username and password to check if the user is logged in. When a session gets started a cookie will be created with the name PHPSESSID and it will have a value that when the hacker acquires can add it to his browser and get access to the account of that person. Consider the following line of code :
session_start();

$_SESSION['username'] = "sri";

$_SESSION['password'] = "123";

Save it and open the page in your browser and open the cookies manager. You will observe that the following cookie would be created.



And when the hacker gets access to the content of the cookie he can write a piece of code to create a similar cookie on his browser with the same content and he would get access to the account of the user. And this is called session hijacking.

How To Prevent Session Hijacking :


Session hijacking is the most common method for hacking PHP sessions and in order to prevent that you can use the following two methods.

1. Hiding The Cookie :


Well note exactly hiding the cookie. The cookie would be visible by using this method but one cannot access it via browser script languages such as JavaScript. And so the user would be safe. To do so add the following piece of code right after you start the session.
ini_set('session.cookie_httponly', true);

2. Restricting with IP address :


And in this method we would be storing the IP from where the user logged in, in our session. And anyone else with IP other than the one in our session would be prevented from accessing the session variables. To do so add the following piece of code.
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; //add this before logging in the user

if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']){   //add this check on top of every page shown after user logs in

session_unset();

session_destroy();

}

else{

//rest of your code

}

Working :



These two are the most effective ways for preventing session hijacking. Follow these and the hackers would be having a hard time. Happy Coding :D

Friday, April 18, 2014

Hello there. The meaning of a blog is changing these days. Almost every single blog out there today has a magazine layout. And every magazine too turned into something which we call it a blog. And so here in this post I'll be teaching you on how to create a blog layout using HTML and CSS.


What will we be dealing with :

Well we will be creating a single post layout using HTML and CSS and it would be having a sidebar too and it would be completely responsive. And so lets get started and see on how to do that.

Things you need :



  1. A text editor

  2. Bootstrap (We will be seeing on how to get that)

Downloading Bootstrap :


Bootstrap is a CSS framework from Twitter  and it is open source and has the best grid system I've ever seen. You can download that from the link.

DOWNLOAD BOOTSTRAP


Download Bootstrap. Extract and open the folder that you extracted. Open the CSS folder and place the bootstrap.css in the folder where you would be creating your HTML files.

Getting Started :


When you get into a site the first thing that you would take a look at is the header of the site. And so we would be creating it first. And the result would be something like the following one.





And so to get something like this type in the following and save it as index.html in the same folder where you saved your boostrap.css file.
<link rel = "stylesheet" href = "bootstrap.css">

<title>My New Site</title>

<body style = "background-color: #f1f1f1;">

<div style = "background-color: #365e99; height: 70px; padding: 20px; color: #fff;">
<text style = "font-family: Muli; font-size: 18px;">MY NEW SITE</text>
<ul style = "list-style: none; float: right; margin-top: 5px; font-weight: bold; font-family: Muli;">
<li style = "margin-right: 15px; display: inline;">HOME</li>
<li style = "margin-right: 15px; display: inline;">ABOUT</li>
<li style = "display: inline;">CONTACT</li>
</ul>
</div>


Edit the things that you need to do in the above code.

Post Body :


And so the next thing that you need to do is to take care of the main post area. And this is where you blog post and the widget are would go.

The output would look like the following :



And so open up your index.html file which you saved in the previous part and paste the following code after what we typed earlier.
<br><br>

<style>

p {
font-family: Muli;
font-size: 15px;
line-height: 2em;
}

</style>

<link href='http://fonts.googleapis.com/css?family=Monda' rel='stylesheet' type='text/css'>
<div class = "container">
<div class = "rows">
<div class = "col-md-8" style = "background-color: #fff; padding: 15px;">
<b><text style = "font-family: Monda; font-size: 30px; color: #364957;">How To Remove Background Noise From Video</text></b><br><br>
<i>Posted By Admin</i><br><hr>

<p>
Ever wondered how some of the youtube videos got no background noise ? The answer is simple and there are two possible answer. One. They are using a good microphone with great noise cancellation. Two. They know how to remove background noise from their audio or video. And this is exactly what we are going to see in this post.
</p>

<h3 style = "font-family: Monda;">
Convert Video To MP3 :
</h3>

<p>
If you are having a video file then you need to first convert that video file into an MP3 file and we would be removing the background noise from this mp3 file. To do so take a loot at the following steps.
<br><br>
1. Download & Install Free Video To MP3 Converter.
<br>
2. Open it up and browse for the video for which you want to remove background noise.
<br><br>

<center><img src = "image.png"></center>
<br><br>
3. Choose the destination folder and click Convert.
<br><br>
The process will proceed and the video will get converted to an MP3 file.
<br><br><hr>

<!-- Replace this with your comment box code... to get comment code visit disqus.com  -->
</p>

</div>

<div class = "col-md-3 col-md-offset-1" style = "background-color: #fff; padding: 15px; padding-top: 0px;">
<h3 style = "font-family: Monda;">Calender</h3>
<img src = "calender.png" width = "100%">
<br>

<h3 style = "font-family: Monda;">Twitter</h3>
<a class="twitter-timeline" href="https://twitter.com/ms_srivathsan" data-widget-id="455611402590236672">Tweets by @ms_srivathsan</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<h3 style = "font-family: Monda;">Facebook Like Box</h3>

<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fxprowebtech&amp;width=250&amp;height=290&amp;colorscheme=light&amp;show_faces=true&amp;header=true&amp;stream=false&amp;show_border=true" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:250px; height:290px;" allowTransparency="true"></iframe>
</div>

</div>
</div>

<br><br>

</body>

Well !! And that's it. Save it and open up your browser and you will have your site ready. The only thing that you need to do is to replace the content that I have typed with yours. And Hurray ! Your site is up and ready now ! Happy Blogging :D

Wednesday, April 16, 2014

Fonts ! They take up an important role in web design. But sill most of the good looking fonts cost you some money. And so here is a list of top free good looking fonts.


1. Muli :


This is the font that you are looking right now ! I mean this is the font that I am using for my web posts. It can be downloaded from the Google Font library.



Import Code :
@import url(http://fonts.googleapis.com/css?family=Muli);

2. Ubuntu :




Import Code :

@import url(http://fonts.googleapis.com/css?family=Ubuntu);

3. Roboto :



Import Code :

@import url(http://fonts.googleapis.com/css?family=Roboto);

4. Ruluko :




Import Code :

@import url(http://fonts.googleapis.com/css?family=Ruluko);

5. McLaren :



Import Code :



@import url(http://fonts.googleapis.com/css?family=McLaren);


And that's the end of the list of free good looking fonts ! Well these are not all. Still want more ? There are tons of free fonts that you can use from the Google Font library. Just go to their site and choose one according to your need. Happy Designing :D

Monday, April 14, 2014

If you are a web developer you should have probably heard of this .htaccess file. Here in this post I'll be dealing with some of the basics of .htaccess file and a few tricks that you could do with your .htaccess file.



What is a .htaccess file ?


Well a .htaccess file is usually placed in the root directory of your server and it contains a few lines of plain text and each line on the file is called as a directive. These directives gets applied to all the folders and sub folders present in the root (i.e. where the .htaccess file is presemt).

How to create a .htaccess file ?


A .htaccess file can be created in the same way any other text document or an HTML document is created. You can create it with your text editor and by saving it with the name .htaccess (Note the period preceding htaccess).  After saving the file you need to upload it to the root of your server. Note that in most of the servers .htaccess file is already present but it would be hidden. If that is the case then edit the file that is already present instead of uploading a new one.

Does every web developer know to code the .htaccess file ?


No. Not every web developer needs to know to code the .htaccess file. This file could be used in some cases like if you want to display a custom 404 page instead of the default one by server or if you want to handle moved or removed pages. Snippets of .htaccess code is available all over the web and you can just copy and paste into yours instead of knowing to code it. But still happiness is knowing to code your own .htaccess file :)

A Few Tips that You Should Know :


And here is a list of 5 tips that would be handy for you in case you are wanting to code your own .htaccess file. It's worth taking a look.

1. Custom 404 Pages :


Most of the servers display a very ugly 404 page. Do you want to change that ? Then this tip would be of much use to you. Create your 404 page and upload it to your server. Open the .htaccess file and add the following line to it :


ErrorDocument 404 /404.html


And replace the 404.html with the path to your 404 page that you just uploaded.

2. 301 Redirects :


Just moved to a new site ? Well this .htaccess directive would help you to redirect the visitors to your old site to your new one. Just open the .htaccess file of your old site (i.e the one present in the root directory of your old site) and add the following line. Replace xproweb.org with the URL of your new site.

Redirect 301 / http://www.xproweb.org/

3. Preventing Users from Browsing the directories :


If a folder in your server doesn't have a index page then the users can browse the directories that are present inside the folder. So to prevent this add the following line to your .htaccess file :

Option All -Indexes

4. Custom Error Pages :


This one if similar to the first tip. Using this you can display custom error pages for 401 (Unauthorized entry), 403 (Forbidden), 500 (Internal Server Error)


ErrorDocument 401 /401.php #Unauthorized

ErrorDocument 403 /403.php #Forbidden

ErrorDocument 404 /404.php #Not Found

ErrorDocument 500 /500.php #Internal Server Error


5. Hiding certain extensions from Directory Browsing :


If you allow directory browsing for your user but still if you want to hide files of certain extensions from the user then this would be useful. Add the following line and you can add more extensions in the same pattern.

IndexIgnore *.jpg *.gif

And so yes, these are the basics of the .htaccess file and you can learn more about these files here. Happy Coding :)