ariandy This user has been deleted
|
macamana kita nak buat bila kita login lepas tu page yang seterusnya terus papar nama orang yang log in tu.ade sapa2 leh tlong tak macamana nak buat coding tu |
|
|
|
|
|
|
|
Banyak cara bleh buat...name yg nk display tu username atau nama penuh yg store dlm database ?? Kalo username...ambik direct dr page yg ade form utk login tu pun bulih... |
|
|
|
|
|
|
|
login page:
<?
session_start(); // wajib ada, mesti di line pertama
... login functions
... if login ok then:
$_SESSION["username"] = "<username yang nak papar>";
...
...
?>
page-page lain:
<?
session_start(); // wajib ada, mesti di line pertama
...
echo ($_SESSION["username"]); // untuk papar kembali user name yang tadi
...
...
?> |
|
|
|
|
|
|
ariandy This user has been deleted
|
tak tahu le
leh tak tunjuk coding yang penuh shanaz.
ini coding Loginprocess saya
<?PHP
if ((empty($username))||(empty($password)))
{
die ("Please fill in username and password");
}
$connect=mysql_connect('localhost');
mysql_select_db('Fis');
$SQLcommand = "SELECT * FROM daftar WHERE Katanama = '$username'";
$result = mysql_query($SQLcommand, $connect);
while($row = mysql_fetch_array($result))
{
$tmpUserName = $row["Katanama"];
$tmpPassword = $row["Katalaluan"];
}
if (!(($username==$tmpUserName)&&($password==$tmpPassword)))
{
echo "<h1>Wrong username and password!</h1>";
}
else
{
session_start();
session_register("username");
session_encode();
$url = "Location: home1.php?PHPSESSID=".$PHPSESSID;
header($url);
}
?>
lepas login dia pi kepage seterusnya.lepas tu nama dipapar disitu.so coding tu nak letak dimana |
|
|
|
|
|
|
|
Reply #4 ariandy's post
My comments:
1. session_start() hanya boleh diletak di permulaan setiap page PHP (first PHP code in a PHP file). Tak berguna kalau dia di-embed di tengah-tengah PHP code.
2. I am assuming $username and $password values are taken from $_REQUEST (or $_GET / $_POST) arrays, depending on which version of PHP you are using.
3. Your SQL statement can be optimised. Instead of just setting the criteria for username, you can include the password in as well. Like so:
- $SQLCommand = "select * from daftar where katanama = '$username' and katalaluan = '$password';
Copy the Code
That way, the $row variable can be implemented as follows:
- $SQLCommand = ...;
- $result = mysql_query($SQLCommand);
- $row = mysql_fetch_array($result);
- if ($row == null || $row == "") {
- echo ("Wrong username or password");
- }
- else {
- // ... baca bahagian seterusnya ..
- }
Copy the Code
That way u save a few lines of code already.
4. Untuk redirect ke next page, saya tipu PHP into breaking into HTML. Set the session variable. Then in the next page just display the session variable. Code as follows:
- //(loginprocess.php - sambung code yang di atas)
- if ($row == null || $row = "") {
- echo ("Wrong username or password");
- }
- else {
- $_SESSION["username"] = $username; // set a new session variable
- // ni trick saya guna untuk redirect to next page (tak tau efficient ke idak)
- // breaking out of PHP
- ?>
- <!-- in HTML -->
- <meta http-equiv="refresh" content="0;url=home1.php">
- <?
- // back to PHP
- }
- //(home1.php)
- <?
- session_start();
- // if a session has been started in the preceding page,
- // it continues the session, else it creates a new session. That's why
- // it is required in the first line of every PHP page
- // now check whether the session variable username is defined or not
- if (isset($_SESSION["username"])) {
- echo ("Welcome, " . $_SESSION["username"]);
- }
- else {
- echo ("Your session has expired. Please try again.");
- }
- ?>
Copy the Code
Harap dapat membantu.
[ Last edited by shahnazz at 4-8-2006 10:04 AM ] |
|
|
|
|
|
|
ariandy This user has been deleted
|
tak jadi le
shahnaz tak jadi la.dia hanya dapat display satu nama sahaja lepas tu ada error
kasi email saya hantar full sistem untuk anda tengok.tolong ini projek praktikal saya |
|
|
|
|
|
|
|
saya dah hantar PM. nanti reply ye. |
|
|
|
|
|
|
slier This user has been deleted
|
huh...aku baru nak tunjul skill....
baru tengok shannaz explain aku dah penin....
apsal aku buat session senang jer..... |
|
|
|
|
|
|
|
tunjuk la :bgrin:
tiap programmer ada skill-skill yang berbeza... at least boleh compare programiming styles... |
|
|
|
|
|
|
|
aku selalu wat session guna wizard jer hahaha..pemalas.. |
|
|
|
|
|
|
| |
|