Monday 15 April 2013

PHP - Recalling information stored in database using sessions -



PHP - Recalling information stored in database using sessions -

i've got problem when using sessions recall info stored in mysql database. code:

the login page simple, input username , password kind (i know password still plaintext, plan alter later).

<?php $host="localhost"; $user="root"; $pass=""; $db_name="proyek"; $tbl_name="murid"; mysql_connect("$host", "$user", "$pass")or die("cannot connect sql."); mysql_select_db('$db_name'); ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <div id="header"> logo <h1 align="center">title</h1> </div> <br/> <div id="login"> <form id="loginform" name="loginform" method="post" action="checklogin.php"> <table border="0" align="center"> <tr> <td>nis</td> <td></td> <td><input type="text" name="nislogin" id="nislogin"/></td> </tr> <tr> <td>password</td> <td></td> <td><input type="password" name="pwdlogin" id="pwdlogin"/></td> </tr> <tr> <td colspan="3" align="center"><input type="submit" name="loginbutton" id="loginbutton" value="login"/></td> </tr> </table> </form> </div> </body> </html>

and code login check page:

<?php session_start(); $host="localhost"; $user="root"; $pass=""; $db_name="proyek"; $tbl_name="murid"; mysql_connect("$host", "$user", "$pass")or die("cannot connect sql."); mysql_select_db('$db_name'); $nis=($_post['nislogin']); $pwd=($_post['pwdlogin']); $sql="select * murid nis='$nis' , password='$pwd'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { $_session['nislogin']=$nis; $nama=$result['nama']; $_session['nama']=$nama; header("location:index.php"); homecoming true; exit; } else { echo("wrong nis or password."); homecoming false; } ?>

i have entered dummy info in database testing purposes; id, password, name. how can recall database while user login username/id?

i'd display 'hello, name' in next page. help appreciated.

edit: i've edited code based on feedbacks , produces blank; 'hello,' no name.

first of all, since running query on login check page, utilize value session rather post data. also, whenever redirecting, exit script.

edit:

since in development stage, need display error if query fails know why. realized need homecoming associative array access row. seek this.

$sql="select * murid nis='$nis' , password='$pwd'"; $result=mysql_query($sql); if (!$result) { die('invalid query: ' . mysql_error()); } $count=mysql_num_rows($result); if($count==1) { $data=mysql_fetch_assoc($result); // since accessing 1 row, // otherwise set in loop build array. session_start(); $_session['nislogin']=$data['nis']; header("location:index.php"); homecoming true; exit; }

i see session on login page, don't see ever created , don't see purpose.

if(isset($_session['nama'])) { unset($_session['nama']); }

basically need here, start session on index.php , output it.

index.php

session_start(); if(isset($_session['nislogin'])) { $name = $_session['nislogin']; } else { $name = "stranger"; } <body> welcome, <?=$name?> </body>

php session

No comments:

Post a Comment