Results 1 to 13 of 13

Thread: Sape tau main ngan SESSION in PHP ??

  1. #1
    Join Date
    Jul 2002
    Location
    ch3r4$ d4ruL 3h$4n
    Posts
    26
    Rep Power
    0

    Sape tau main ngan SESSION in PHP ??

    Assalamu'alaikum wbr,

    erm.. ada sesape ble tolong? harap sesangat nie ..
    aku tengah develop satu sistem gune PHP..

    tang login form aku dah create n success..
    tapi aku tak gheti camne nak main ngan SESSION..

    so, once login is success, aku redirect ke member's page..
    dr situ aku nak view info but tak tau nak tarik ID dia..

    harap ada sesape dpt membantu..

    Wassalam.
    -Kh4|if4H^4|4m^m4y4-
    Akhirat=CAHAYA,Dunia=BAYANG2.
    Bile kite kejar cahaya,otomatik bayang2 folo

  2. #2
    Join Date
    Jul 2002
    Location
    ch3r4$ d4ruL 3h$4n
    Posts
    26
    Rep Power
    0
    ok nie coding aku.

    login.php

    Code:
      <form action="logindo.php" method="POST">
        <table border="1">
          <tr bgcolor="#993399"> 
            <td colspan="2"> 
              <div align="center"><font color="#FFFFFF">Login</font></div></td>
          </tr>
          <tr> 
            <td width="114">Matrix Number</td>
            <td width="174">: 
              <input name="matrix_no" type="text" id="matrix_no" /></td>
          </tr>
          <tr> 
            <td>Password</td>
            <td>: 
              <input type="password" name="password" /> <input name="submit" type="submit" value="send" /></td>
          </tr>
        </table>
        [ <a href="register.php">register</a>? ] 
      </form>

    logindo.php

    Code:
    <?php 
    session_start(); 
    error_reporting(E_ALL); 
    
    // Connect to the database server 
    $dbcnx = mysql_connect('localhost', 'root', '') 
                 or die('<p>Unable to connect to the database server at this time.<br />Error: ' . mysql_error() . '</p>'); 
    
    // Select the huh database 
    mysql_select_db('profile') 
                 or die('<p>Unable to locate the profile database at this time.<br />Error: ' . mysql_error() . '</p>'); 
    
    $matrix_no = strip_tags(trim($_POST['matrix_no'])); 
    $password = strip_tags(trim($_POST['password'])); 
    
    $sql = "SELECT matrix_no, password, user_id 
              FROM userinfo 
             WHERE matrix_no = '$matrix_no' 
               AND password = '$password' 
             LIMIT 0,1"; 
    
    $result = mysql_query($sql) 
                 or die('<p>Unable to query the profile database at this time.<br />Error: ' . mysql_error() . '</p>'); 
    
    $row = mysql_fetch_array($result); 
    echo '<p>sql: ' , $sql , '<br />Matrix Number: ' , $row['matrix_no'] , '<br />Password: ' , $row['password'] , '</p>'; 
    
    if(mysql_num_rows($result) == 1) // mysql_num_rows() returns the record count from the query result.  you want one (1) matching record to verify the login was successful. 
    { 
         $_SESSION['loggedin'] = 1; // Setting session var 'loggedin' to true 
    	 $_SESSION['user_id'] = $user_id;
         header('Location:member.php'); 
         exit; 
    } 
    else 
    { 
         header('Location:error.php'); // Redirect to error page, as a matching record was not found in the table. 
         exit;  // always 'exit' 
    } 
    ?>
    login success...
    erm... btul ke aku set session tu?
    dan ape coding yg perlu aku letak tuk setiap page for registration user je yg ble xs?
    -Kh4|if4H^4|4m^m4y4-
    Akhirat=CAHAYA,Dunia=BAYANG2.
    Bile kite kejar cahaya,otomatik bayang2 folo

  3. #3
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    249
    have you refer official PHP manual for SESSION help? There are explaination and example of code inside.

    You can get it from http://www.php.net

  4. #4
    Join Date
    Jul 2002
    Location
    ch3r4$ d4ruL 3h$4n
    Posts
    26
    Rep Power
    0
    erm.. still tak brape faham la..
    ble bg contoh coding tak?

    dan ape coding yg perlu diletakkan di awal page yg hanya registered user je ble view?
    -Kh4|if4H^4|4m^m4y4-
    Akhirat=CAHAYA,Dunia=BAYANG2.
    Bile kite kejar cahaya,otomatik bayang2 folo

  5. #5
    Join Date
    Sep 2004
    Location
    Shah Alam
    Posts
    68
    Rep Power
    185
    kena faham dulu konsep session..

  6. #6
    Join Date
    Jul 2004
    Location
    SJ, Selangor
    Posts
    189
    Rep Power
    188
    i'll explain very very briefly here.

    I assume that you already have a form for username and password which could be a html file. then when clicked on login the action should be pointing to a php file.
    In the php file u will do this:
    Firstly, check whether the username & password matched if yes
    then u have to start a session.
    Later, u have to create a session and store value into it.
    next, on every page you must start with session_start(); (only for those pages that requires login).
    after that, u put few lines of codes after the session_start() to check whether the session variable was having a value that you assigned during the first page (only for pages that requires login).
    if there is value continue else die(); so that the page will not be displayed.

  7. #7
    Join Date
    Jun 2001
    Location
    Mystic Kingdoms
    Posts
    2,749
    Rep Power
    277
    Disini tutorial login, session dalam BM.
    oleh sdr rokawa kalau tak silap..

    Senang dan mudah difahami..
    yang penting punyai ketajaman minda dan tahap sabar berusaha yang tinggi

    http://www.php.net.my/211-Membina-ha...an-laluan.html

  8. #8
    Join Date
    Jul 2002
    Location
    ch3r4$ d4ruL 3h$4n
    Posts
    26
    Rep Power
    0
    ok.. alhamdulillah.. session dah success..

    ok la nie bile user dah success login.. akan ada satu button tuk apply hostel.. just satu list down je... then bila dia submit, failed la... data tak masuk dlm databse..

    FYI, aku just create satu table je.. and now just nak update + masukkan field (hostel) tadi tu je.. nie coding aku :


    apply.php
    <?php
    session_start();
    if (!$_SESSION[matrix_no])
    {
    print "<p align=center><font color=red>Access Denied</font></p>";
    //header("Location:xs_denied.php");
    include ("login.php");
    }
    else
    {
    ?>
    <html>
    <head>
    <title>Apply Hostel</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body><form name="form1" method="post" action="applydo.php">
    <h1 align="center">Apply Hostel</h1>
    <table width="217" border="0" align="center" cellspacing="0">
    <tr>
    <td width="98">Select hostel</td>
    <td width="115">:
    <select name="hostel" id="hostel">
    <option value="-">-</option>
    <option value="Perindu">Perindu</option>
    <option value="Anggerik">Anggerik</option>
    </select></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><br> <input type="submit" name="Submit" value="Submit"> <input type="hidden" name="hiddenField">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <?php
    }
    ?>
    applydo.php
    <?php
    session_start();

    if (!$_SESSION[matrix_no])
    {
    print "<p align=center><font color=red>Access Denied</font></p>";
    //header("Location:xs_denied.php");
    include ("login.php");
    }

    else
    {
    ?>
    <html><title>Apply Hostel</title>
    <body>
    <?php
    $conn = mysql_connect ("localhost", "root", "");
    mysql_select_db ("profile", $conn);
    //$sql = "UPDATE userinfo SET hostel = ($hostel) WHERE matrix_no=$_SESSION[matrix_no]";
    //$sql="UPDATE userinfo SET hostel ='"$hostel"' WHERE matrix_no = '"$_SESSION'[matrix_no']"'";
    $hostel = strip_tags(trim($_POST['hostel']));

    $sql="UPDATE userinfo SET hostel = ($hostel) WHERE matrix_no = $_SESSION[matrix_no]";

    if (mysql_query ($sql, $conn))
    {
    echo "<h1>Your application will be process!</h1>";

    print ("<br><br>You'd apply hostel : <b>");
    print ($hostel);
    print ("</b>");

    }
    else
    { echo"<h1>something went wrong</h1> <br> <input type='button' value='Back' onclick=history.go(-1)>"; }
    ?>
    </body>
    </html>
    <?php
    }
    ?>
    bile members click submit.. dia akan gi terus ke ELSE dan papar mesej something went wrong.
    Harap dapat membantu..
    -Kh4|if4H^4|4m^m4y4-
    Akhirat=CAHAYA,Dunia=BAYANG2.
    Bile kite kejar cahaya,otomatik bayang2 folo

  9. #9
    Join Date
    Apr 2002
    Location
    kuantan, pahang
    Posts
    58
    Rep Power
    213
    check syntax, agak berterabur sket la

    1)kalau array tu jangan lupe letak ( " ) atau ( ' ), takpe kalau tak letak pun PHP akan assumed. sebaiknye letak la

    2)lagi, kalau kita nak letak array dalam string. kene letak { }
    Code:
    $sql="UPDATE userinfo SET hostel = '$hostel' WHERE matrix_no = '{$_SESSION['matrix_no']}'";
    3)try to make your code clean & tidy

    contohnye kalau dah guna "echo" , guna echo terus, takyah guna "print"

    4)escape character ( \ ) jangan dilupakan

  10. #10
    Join Date
    Feb 2004
    Location
    Klang Bandar Diraja
    Posts
    160
    Rep Power
    194
    aku dah lama tak jenguk php ni.. dekat setengah tahun sbb sambg blaja. takde masa nk buat progg. tapi, kalau aku tak silap, kat applydo.php tu ko tak hold value yg ko pass dr prev page tuh.. kalau aku tak silap la.. kalau silap, sorry beb!
    To follow the path:
    Look to the master,
    Follow the master,
    Walk with the master,
    See through the master,
    Become the master.

  11. #11
    Join Date
    Jul 2002
    Location
    ch3r4$ d4ruL 3h$4n
    Posts
    26
    Rep Power
    0
    ok.. prob solve gak akhirnye..
    fuh.. mencabar gak awal2 nak blaja nie..
    aku tukar code jadi cam nie :

    Code:
    $sql="UPDATE userinfo SET hostel = '$hostel' WHERE matrix_no = $_SESSION[matrix_no]";
    mekaseh atas perhatian anda semua..
    -Kh4|if4H^4|4m^m4y4-
    Akhirat=CAHAYA,Dunia=BAYANG2.
    Bile kite kejar cahaya,otomatik bayang2 folo

  12. #12
    Join Date
    Nov 2003
    Location
    malaysia
    Posts
    25
    Rep Power
    0

    susah sanget

    Guna jek dreamweaver untuk buat code session

  13. #13
    Join Date
    Oct 2004
    Location
    Johor Bahru, Malaysia
    Posts
    41
    Rep Power
    0
    well... session ni senang jer.. kalau faham concept dia.

    hmm.. ni code paling simple untuk session..


    if (!$_SESSION['username']) {
    include("login.php");
    }
    else {
    include("page.php");
    }


    hmm.. katakan.. lepas seseorang login.. dia akan diforward ke page page.php... kalau non member.. dia akan.. ke page.. login.php..

    ni bukan code complex... ni untuk.. strict kan page ke member / non-member..:P
    Last edited by YajivMalhotra; 10-10-2004 at 03:21 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. www.vivientheplay.org/main.htm
    By simmu in forum Websites Review and Suggestion
    Replies: 8
    Last Post: 17-05-2005, 05:24 PM
  2. php bleh run ngan pws kan?
    By therion in forum Website Programming
    Replies: 34
    Last Post: 27-01-2004, 05:47 PM
  3. Main Game kat sini
    By duwe in forum Websites Review and Suggestion
    Replies: 2
    Last Post: 25-12-2003, 01:44 AM
  4. Replies: 7
    Last Post: 12-08-2002, 11:30 AM
  5. jom main bola.....
    By donut in forum Mamak Stall
    Replies: 1
    Last Post: 24-06-2002, 11:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112