WHAT'S NEW?
Showing posts with label phpscripts. Show all posts
Showing posts with label phpscripts. Show all posts
In this tutorial, we will see how sessions works in addition to storing and getting records from database. then checking records are they available or not.



this tutorial has fondamentale of creating login and register forms and make them work.



First take a look at the demo:

So for the HTML and CSS part we will work on an already made Form download it from here:
  

Download files to your computer, run your localhost server and let's start coding.

PART I: Creation of database

So what we are going to need is two fields, username and password.
Create a new database and call it -log- and inside of it table and call it -users- then two fields -username- and -password- like that:


 You can created this on your own way or just download the SQL file from here and import it into you SGBD.

PART II: Connecting database with the script

So for connecting all script pages we need to create another file db.php and include it to all other pages, so create a new db.php file and write down this code.





<?php
$con = mysqli_connect("localhost","root","","log");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
?>

To understand the code please check the previous tutorial from here:

http://www.digitalservicesmarket.biz/2015/09/source-files-how-to-create-commenting.html

now we should include the db.php or the connection file into all other pages, all what we are going to do is to past the next code at the top of the following files: login.php, index.php, register.php and not need to add it to the logout.php:



<?php include('db.php'); ?>

Now database connection part is finished Yaay :D

PART III: Register form add new users to the table

in this part we will manage new users, so users need to give there informations ( username and password) check them and store them in database.

so open the register.php file and let's do something work.

let's edit our form and make the action after sending form to regsiter.php?new=1, it a little complicated to understand why now but at the finish of the tutorial you'll understand, don'y worry.

the form code should be like that:


<form method="post" action="register.php?new=1" class="login">

Now we should make register.php?new=1 page get data and store it in the database.

so we make a call to the page, insert this code into your register.php file:


<?php if (isset($_GET['new']) && is_numeric($_GET['new']) && $_GET['new']==1){
  
  // parameters of register.php?new=1 
 } ?>

So the explanation of the code is  if the page has new as an argument that is ?new and new equal to a number that is 1 do the following instruction, here we made a call to the new page inside the register page.
so the instruction are to get data from the registering form then, check if they are not empty and store them at the database using SQL query.
let's translate that to a php code first get data from form.



//1st getting data
$username = (htmlspecialchars($_POST['login']));
$password = (htmlspecialchars($_POST['password']));

And we check if they are not empty with the classic function if:



if($username=='' || $password==''){ // if the two fields are empty
   echo 'username and password are required';
  }else{
   //if everything is okey
   
  }
Good now we use MySqli query to put them in database.



mysqli_query($con,"INSERT users SET username='$username', password='$password'")
 or die(mysql_error()); 

So after data is stored user should be redirected into login.php using headers so all code will be:



<?php if (isset($_GET['new']) && is_numeric($_GET['new']) && $_GET['new']==1){
  
  // parameters of register.php?new=1 
  //1st getting data
  $username = (htmlspecialchars($_POST['login']));
  $password = (htmlspecialchars($_POST['password']));
  
  if($username=='' || $password==''){ // if the two fields are empty
   echo 'username and password are required';
  }else{
   //if everything is okey
   mysqli_query($con,"INSERT users SET username='$username', password='$password'")
 or die(mysql_error()); 
 echo 'good';
 echo $username;
  }
  
 } ?>

to read more about how we store data please check this previous tutorial:

(Source files) How to create a commenting system using php and MySql (php tutorial)


PART III: Login form and checking username and password

In this part we will get data from login.php form and testing if data are in database or not.
so in the login.php we made some changes here like the action of the form we will make it to login.php?check=1

so we are going to do the same as we have done in register part:


<?php if (isset($_GET['check']) && is_numeric($_GET['check']) && $_GET['check']==1){
  
  
  //1st getting data
  $username = (htmlspecialchars($_POST['login']));
  $password = (htmlspecialchars($_POST['password']));
  
  if($username=='' || $password==''){ // if the two fields are empty
   echo 'username and password are required to log in';
  }else{
   //if everything is okey let's check if they are exist in data base
   
  }
  
 } ?>

now in the else{} part we will check if they are in database, first we need to explore database and check if they are exist with query.



$result=mysqli_query($con,"SELECT * FROM users WHERE username='$username' AND password='$password'");
            $login = $result->num_rows;
This query calculate how many rows in database has the username and password the user intred so if login equal to 0, the password and user are not in database and if it equal to 1 so user and pass are in.

So if login equal to 1 user must login, else he don't have permission.

translate into php.




if($login==1){
    // Good you are logged in redirect to home page
   }else{
    // check your username and password and try again
   }
PART IV: Sessions and login details

what are session in php?
When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.

so we need to start session and store username and password in session variables, 



session_start();
    $_SESSION["username"]=$username;
    $_SESSION["password"]=$password;

now username and password are stored in two session variables then we redirect users to index.php.

in the else part we just advising users to recheck ther details.
All php code would be like that:


<?php if (isset($_GET['check']) && is_numeric($_GET['check']) && $_GET['check']==1){
  
  // parameters of register.php?new=1 
  //1st getting data
  $username = (htmlspecialchars($_POST['login']));
  $password = (htmlspecialchars($_POST['password']));
  
  if($username=='' || $password==''){ // if the two fields are empty
   echo 'username and password are required to log in';
  }else{
   //if everything is okey let's check if they are exist in data base
   $result=mysqli_query($con,"SELECT * FROM users WHERE username='$username' AND password='$password'");
            $login = $result->num_rows;
   
   if($login==1){
    // Good you are logged in redirect to home page
    session_start();
    $_SESSION["username"]=$username;
    $_SESSION["password"]=$password;
    header('location: index.php');
   }else{
    // check your username and password and try again
    echo 'check details and try again';
   }
  }
  
 } ?> 
PART V: Home page security

For the home page or index.php, only logged in user could get into it. let's add security codes:

in index.php code we start the session and try to test if session variable are empty so user must login else user can stay.

So open the index.php file and let's write down this small code:



<?php
session_start();
if($_SESSION['username']=='' || $_SESSION['password']==''){
 header('location: login.php');
}
?>
Great!
PART VI: Destroying sessions and logout function
for the logout page it's just about destroying sessions and variable so we will put this code in the logout.php file:



<?php
// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 
header('location: index.php'); /*this is to redirect to the main page as the session is
destroyed it will be redirected to the login page */
?>

so everything is works good now.
PART VI: Additionnelle tricks must be added.

Now if someone has potted the same username as another this part should be managed.
so what we are going to check if username already exist in database, we tell user to choose another one, let's go back to register.php and add this function.
query is to select all rows where username as it puted by user if number of rows is not equal to 0 means that user already exist so check that code you'll understand, so all php code in register.php would be like that:





<?php if (isset($_GET['new']) && is_numeric($_GET['new']) && $_GET['new']==1){
  
  // parameters of register.php?new=1 
  //1st getting data
  $username = (htmlspecialchars($_POST['login']));
  $password = (htmlspecialchars($_POST['password']));
  
  if($username=='' || $password==''){ // if the two fields are empty
   echo 'username and password are required';
  }else{
   //if everything is okey
   $result=mysqli_query($con,"SELECT * FROM users WHERE username='$username'");
            $users = $result->num_rows;
   if($users!=0){
    echo ' User already exist check another one please';
   }else{
   mysqli_query($con,"INSERT users SET username='$username', password='$password'")
 or die(mysql_error()); 
 echo 'good';
 echo $username;
  }}
  
 } ?> 

Another thing, is people are logged shouldn't get into register page, so the same code as index.php, but this time if session variable are not empty go to the index.



<?php
session_start();
if($_SESSION['username']!='' || $_SESSION['password']!=''){
 header('location: index.php');
}
?>

The same code must be putted in login.php.

Great News this is the final step in this tutorial you have made your own login and register system with php and mysql database.

Download source file and don't forget to share this article.


Don't forget to share this on facebook twitter or google+ and subscribe to get everything new tutorial with source files.



If you have any questions, please feel free to add comments.

and share with us your experience with php and web developement.



Hello, today we are going to create a commenting system using php and MySql database.
Check demo from here:




So for the HTML and CSS part i have attached here the template that we will work on, so this tutorial will be about integrating php and MySQL into that template and make it work

From here Download the HTML and CSS (If you wan the full work check down at the end of the article).
Let's get Started:

Part I : Database creation and environment setting 

So we will create database and table inside to store comments, all what we need is two fields in a table. so we create a database and we call it -cmt-

Inside of this database let's create a table and call it -com- with two fields like this

You can do all this or just download the MySQL Queries to build up the database from here:
Now everything's all right, let's write some php codes.

Part II : Database Connection

First thing every php developer start with, IS TO CONNECT DATABASE WITH SCRIPT

So for the connection we will create another php file and write this small script on it :

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

then we replaced our database details in the previous code and we get:




<?php
$con = mysqli_connect("localhost","root","","cmt");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


Then save the file as db.php newt to index.html file.

Now we include the db.php into html one using include function.

<?php
include ('db.php');
?>

SO IMPORTANT:


Another thing is to change the extension of index.html to index.php so the php file can read php codes.

Part III : Store data into database

Open the index.php file and we will get data from the form using $_POST function, and it function that when i click on, and we will use it on submit button of the form to work as: when i click on submit { GET DATA FROM FORM }, it's an easy algorithmic.


<?php

$_POST['name']; // this get data from name input in form

?>

Now let's get the form values and store them into variables:

so we create a new file and call it cmt.php and write down this code:

cmt.php




<?php
     
                 $name = (htmlspecialchars($_POST['name'])); 
     $comment = (htmlspecialchars($_POST['comment']));

?>

Now values of name and comment are stored in the two variables $name and $comment.

we call the db.php file to cmt.php again and we use the sql query to store the two variables values in database so the final code will be like that.



<?php
    include ('db.php'); 
                 $name = (htmlspecialchars($_POST['name'])); 
     $comment = (htmlspecialchars($_POST['comment']));
                $sql = "INSERT INTO com (name, comment)
     VALUES ($name, $comment)";
// Here we are going to check if records are added to database or not

if ($con->query($sql) === TRUE) { // if records has been add we will be redericted to home page
    echo "New record created successfully";
header('Location: index.php');
 
} else {// if not show error
    echo "Error: " . $sql . "<br>" . $con->error;
}
?>

Everythings is explained in the comments of the php code.

Part IV : Get records from database and show as comments

So we go back to index.php file and we will use this code in comments section:




><?php
$sql = "SELECT * FROM com";
$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
  $name=$row['name'];
  $comment=$row['comment'];
       
    }
} else {
    echo "There is no comments";
}?>

Now from the code data in table are stored in the two new variables $name and $comment.

We will do some replacement in the latest comment section and code will be like this :



<h2>Latest comments</h2>
                    </div><?php
$sql = "SELECT * FROM com";
$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
  $name=$row['name'];
  $comment=$row['comment'];
       ?>
     <div class="rpost">
                        <p><a href="#19"><?php echo $comment; ?></a> <br/>
                        <a href="#20"><?php echo $name; ?></a></p>
                    </div>
                    
    
    <?php
    }
} else {?>
<div class="rpost">
                        <p><a href="#19">There is no comments</a> <br/>
                       
                    </div>
   <?php 
}?>
                   
                    

                    
                </div>

So all code of index.php



<?php
include ('db.php');
?>

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>Commenting system</title>
    
    
    
    
        <link rel="stylesheet" href="css/style.css">

    
    
    
  </head>

  <body>

    <!-- My first HTML & CSS -->

<!-- designed by Riki Tanone: http://drbl.in/gUhL -->

<html>

    <head>

        <meta charset="utf-8">

        <title>Let's leave comments</title>
        <meta name="description" content="This is a trial">
        <meta name="author" content="jlalovi">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">   
        
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/main.css">
        <link rel="stylesheet" href="http://i.icomoon.io/public/temp/90320a65cd/UntitledProject2/style.css">

    </head>

    <body>
 
        <div id="main-container">
            <div id="left-container"> <!-- Left part -->
                <div id="newPost" class="container"> <!-- Add New Post -->

                    <div class="side bar">
                        <ul>
                            
                            <li><a href="" id="document"><span class="fontawesome-file-alt"></span></a></li>
                            
                        </ul>
                    </div>

                    <div class="newPostContent">
                        <h1>Add New Comment</h1>
      
      

                          
        
   <form method='POST' action='cmt.php'>     
                        <input type="text" name="name" placeholder="Your name" id="post-title"> 

                       
                        <textarea name="comment" class="post-body"></textarea>

                        
                        <input type="submit" value="submit" class="btn"></a>
      </form>
                    </div>                   
                </div>

                

               
            </div>

            <div id="middle-container"> <!-- Middle Part -->
                <div id="relatedPosts" class="container"> <!-- Related Posts -->
                    <div class="bar title-bar">
                        <h2>Latest comments</h2>
                    </div><?php
$sql = "SELECT * FROM com";
$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
  $name=$row['name'];
  $comment=$row['comment'];
       ?>
     <div class="rpost">
                        <p><a href="#19"><?php echo $comment; ?></a> <br/>
                        <a href="#20"><?php echo $name; ?></a></p>
                    </div>
                    
    
    <?php
    }
} else {?>
<div class="rpost">
                        <p><a href="#19">There is no comments</a> <br/>
                       
                    </div>
   <?php 
}?>
                   
                    

                    
                </div>

                <div id="tags" class="container"> <!-- Tags -->
                    <div class="bar title-bar">
                        <h2>About</h2>
                    </div>
                    <br><br>
                    <p>This is created for a commenting with php tutorial by <br>
     Www.digitalservicesmarket.biz</p>
                </div>

                
                </div>
            </div>
        </div>
    </body>
</html>
    
    
    
    
    
  </body>
</html>

Now everythings is on work .

download the source files now:


Don't forget to share this on facebook twitter or google+ and subscribe to get everything new tutorial with source files.

If you have any questions, please feel free to add comments.

may you'll be interested downloading others of our free scripts:

http://www.digitalservicesmarket.biz/2015/06/managing-it-park-php-script-ready-to.html


 Free Agency website management for freelancers and teams, is now For free, an open source

Free Agency is compatible and responsive,

The ability to add paypal is now added.

For Admin details please contact us.

Check The Demo from here: http://mjptogramation.orgfree.com/freea/

A look inside the zipped file.

Free agency require the creation of a new database under MySQL 4 or highter.

easy to use and manage.
Secured.
Open sourced for developement.

And more features check them inside.

More:




This is some php free script you will use in the future, an will help you learn more about php and mysql and mysqli querys.


1- Timeline:

Timeline Script to easily add a fancy, interactive timeline to your web project. This can be a great way for a business to present their history, special events, development in number of employees, strategic clients, expansion, turnover etc. 

Download - Demo

2-Subscribe form
Add a fancy newsletter subscription form on your website and collect users' names and email addresses. With our free Subscription Form script you can

- collect subscribers details in MySQL database
- add Name and Email for each subscriber
- record Date and time when subscribed
- have an unsubscribe feature so people can unsubscribe your list
- save date and time when each user unsubscribed

Download - Demo

3-User login

Install a simple user login script to your website to protect your website content. The PHP Login Script is simple, easy and free login script by PHPjabbers. 

- MySQL is used to store the database.
- Custom messages to each user upon login.
- Nice and clean login form.

Click the above tab Demo to try the PHP User Login Script. Use the following details to log in: 

email: demo1@demo.com
password: pass

OR

email: demo2@demo.com
password: pass 

Download the simple PHP Login Script for FREE. Note you need to be logged in to download the user login script.

Download - Demo


We have created a manager of IT park with PHP, and today we will share it with you. i wish if you could learn more from it.

This is not a contractual page



Langages:


  • PHP
  • MYSQLI
  • HTML
  • CSS
The script is created to give responses of some answers like relations betwin tables, and how to add relations using mysqli query etc.

Take a closer look you will love it.


PS: The language of the website is French but coding comments are in english.

From mediafire

Freelancing, Business, CMS, developers, designers
Inline preview.

On a promotional offer, Free Agency website management for freelancers and teams, only for 4$.

Free Agency is compatible and responsive,

The ability to add paypal is now added.

For Admin details please contact us.

Check The Demo from here: http://mjptogramation.orgfree.com/freea/




Buy now with paypal:


Your produc



Overview:
Overview, free agency, freelancers, business

contact support : anassethind@gmail.com

Buy now with paypal:


Your produc