Wednesday, June 19, 2013

PHP SESSION for Login (MySQL and Oracle scripte)

Hello, now i want to share about little thing that i just know.

this is about web programing PHP, database (MySQL and Oracle).
why 2 database?
because i have task to make web program and connected to Oracle database. And previously i didn't know how to connected Oracle database on Web program using PHP. So i'm tried make web that connected to MySQL first. And then i'm started find way how to do that.
1st day . . .
i'm tried and failed, many way i'm tried and still failed. i asked my lecturer and she asked me back "are you you seriously ask that?"(in indonesian language)
-_- i'm shy and not ask anymore.

2nd day . . .
In the morning i starting find again, and it make me headache.
i don't know why it didn't work on my web, what's wrong with my web???
and at 03:00 PM i'm still tried, and...
YESSSSS, IT WORK.
hehehehehe, i'm happy can do that, and now i want to share this.

ok, this is my code when i used MySQL database:

make database on your phpmyadmin:

- database name: uas
- table name: user
- column:
      user_id varchar(15)
      username varchar(30)
      password varchar(15)
- insert 1 until 2 row for test it.

and this is the code:

login.php: (login form)
<script>
//javascipt validation username and password
    function verificationLogin(){
        var userName = document.forms['login']['txtUsername'].value;
        var password = document.forms['login']['txtPassword'].value;
               
        if (userName == ""){
            alert ("username empty");
            return false;
        }
        else{
            if (password == ""){
                alert ("password empty");
                return false;
            }
            else{
                return true;
            }
        }
    }
</script>
<?php
//check session already filled or not
    if(isset($_SESSION['username'])){
        echo '<p>you are logged in</p>';
    }
    else{
        echo '<form name="login" method="POST" action="login_check.php">';
        echo '    <table>';
        echo '        <tr>';
        echo '            <td>Username</td>';
        echo '            <td>:</td>';
        echo '            <td><input type="text" name="txtUsername" /></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td>Password</td>';
        echo '            <td>:</td>';
        echo '            <td><input type="password" name="txtPassword" /></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td colspan="3"></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td colspan="2" ></td>';
        echo '            <td align="left">';
          //button submit call javascript function verificationLogin()
        echo '                <input type="submit" value="     Login     " name id="btnLoggin" onclick="return verificationLogin()"/>';
        echo '            </td>';
        echo '        </tr>';
        echo '    </table>';
        echo '</form>';
    }
//if the verification correct, it will redirect to login_check.php
?>



login_check.php:
<script>
     //javascript to redirect page 
    function succes() {
        window.location = "home.php";
    }
    function fail(){
        window.location = "login_fail.php";
    }
 </script>
<?php

    //javascript on php, to call javascript function
    $succesLogin = '<script type="text/javascript">succes()</script>';
    $failLogin = '<script type="text/javascript">failLogin()</script>';

    //start session
    session_start();
   
//connect to database ('<homepage>', '<database username>','<database password>')
    mysql_connect('localhost', 'root', '');
    mysql_select_db('uas');
    $result = mysql_query("SELECT * FROM user");   
    while($data = mysql_fetch_array($result)) {      

         //if username(form) same with username(from database) and password(form) same with password(from database)
         if($data['username']==$_POST['txtUsername'] && $data['password']==$_POST['txtPassword']){
            $_SESSION['username'] = $data['username'];

            //call javascript function succes
             echo $succesLogin;
        }
    }

       //if nothing match, call javascript function fail
     echo $failLogin;
?>


login_fail.php: (almost same with login.php)
<script>
    function verificationLogin(){
        var userName = document.forms['login']['txtUsername'].value;
        var password = document.forms['login']['txtPassword'].value;
               
        if (userName == ""){
            alert ("username empty");
            return false;
        }
        else{
            if (password == ""){
                alert ("password empty");
                return false;
            }
            else{
                return true;
            }
        }
    }
</script>
<?php
    if(isset($_SESSION['username'])){
        echo '<p>you are logged in</p>';
    }
    else{
        echo '<form name="login" method="POST" action="login_check.php">';
        echo '    <table>';
        //just add this on login_fail.php
        echo '        <tr>';
        echo '            <td colspan="3"><i style="color:red;">please check your username and password!!</i></td>';
        echo '        </tr>';
        //until this
        echo '        <tr>';            
        echo '            <td>Username</td>';
        echo '            <td>:</td>';
        echo '            <td><input type="text" name="txtUsername" /></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td>Password</td>';
        echo '            <td>:</td>';
        echo '            <td><input type="password" name="txtPassword" /></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td colspan="3"></td>';
        echo '        </tr>';
        echo '        <tr>';
        echo '            <td colspan="2" ></td>';
        echo '            <td align="left">';
        echo '                <input type="submit" value="     Login     " name id="btnLoggin" onclick="return verificationLogin()"/>';
        echo '            </td>';
        echo '        </tr>';
        echo '    </table>';
        echo '</form>';
    }
?> 

home.php:
<?php
    echo 'user login: ';
    session_start();
    if (isset($_SESSION['username'])){
        echo $_SESSION['username'];
        echo '&nbsp;&nbsp;<a href="logout.php"><b style="color:green;">logout</b></a>';
    }
    else if(!isset($_SESSION['username'])){
        echo ' - ';
    }
?>
 

logout.php:
<?php
    session_start();
    if(isset($_SESSION['username'])){
        unset($_SESSION['username']);

           //other way to redirect page (php)
        header( 'Location: login.php' );
    }
?>


now i'll share how using Oracle database on your web:
- i assume you have installed oracle 11g
- start this service: oracleDBconsole, oracleService, and OracleOraDB11g_home1TNSListener.
- make new user (create user <username> identified by <password>;).
- give it DBA role, and all privilege
   grant DBA to <username>; and grant all privilege to <username>;
- make table with same name like on MySQL: USER
- column:
      user_id varchar(15)
      username varchar(30)
      password varchar(15)
  insert 1 until 2 row for test it. insert into "USER" values ('<user_id>','<username>','<password>');

- turn off your APACHE (stop it).
- open folder xampp/php and find php.ini
- open php.ini with notepad, find (ctrl + f) this:
    1.)   ;extension=php_oci8.dll  
    2.)   ;extension=php_oci8_11g.dll
    3.)   ;oci8.privileged_connect = Off
    4.)   ;oci8.max_persistent = -1
    5.)   ;oci8.persistent_timeout = -1
    6.)   ;oci8.ping_interval = 60
    7.)   ;oci8.events = Off
    8.)   ;oci8.statement_cache_size = 20
    9.)   ;oci8.default_prefetch = 100 
   10.)  ;oci8.old_oci_close_semantics = Off 
- delete this symbol " ; " and save (ctrl + s).
- start APACHE.
- change the red script on login_check.php:

<script>
     //javascript to redirect page 
    function succes() {
        window.location = "home.php";
    }
    function fail(){
        window.location = "login_fail.php";
    }
 </script>
<?php

    //javascript on php, to call javascript function
    $succesLogin = '<script type="text/javascript">succes()</script>';
    $failLogin = '<script type="text/javascript">failLogin()</script>';

    //start session
    session_start();
   
//connect to database ('<homepage>', '<database username>','<database password>')
    mysql_connect('localhost', 'root', '');
    mysql_select_db('uas');
    $result = mysql_query("SELECT * FROM user");   
    while($data = mysql_fetch_array($result)

         if($data['username']==$_POST['txtUsername'] && $data['password']==$_POST['txtPassword']){
            $_SESSION['username'] = $data['username'];

            //call javascript function succes
             echo $succesLogin;
        }
    }

       //if nothing match, call javascript function fail
     echo $failLogin;
?>

with this:

      //( '<oracle username>' , '<oracle password>, 'localhost/orcl')
    $conn = ocilogon('kiko', 'kikokiko', 'localhost/orcl');
      // "USER" is table name ("<table name>")

    $sql='select * from "USER"';
    $sql_statement = ociparse($conn,$sql);
    ociexecute($sql_statement);
    while ($row = oci_fetch_array($sql_statement)){
       

            //"USERNAME" is column name => ($sql_statement, "<column name>")
         if(ociresult($sql_statement,"USERNAME")==$_POST['txtUsername'] && ociresult($sql_statement,"PASSWORD")==$_POST['txtPassword']){
...
... 
- finish :P

that is my share, and i hope useful for everyone.
thank you

09:06PM, 19 june 2013, At Oost koffie & thee
Ardy Setiawan

reference : 
- IT Underground,
- http://stackoverflow.com/questions/87769/how-to-connect-to-oracle-database
- read and TRY

No comments:

Post a Comment