Jump to content

Recommended Posts

Salam..

Dah lama jugak aku tak masuk tempat ni. Berubah benar cantiknya. Ok, terus ke tajuk utama.

Aku nak buat login page. Yang mana login page ni ditentukan oleh role. So aku ade tiga role iaitu pengurusan, admin dan juga pelajar.

Apa yang aku nak buat, bila user login dia misalnya admin, dia akan ke page A. Kalau user pengurusan login dia akan redirect ke page B dan bila student login dia akan redirect ke page C.

Masalah sekarang bila aku buat dia tak jalan. Semua aku simpan dalam mysql. Ni code aku:
[CODE]
<?php
$host="localhost"; // Host name
$userid="root"; // Mysql username
$password="password"; // Mysql password
$db_name="sistem"; // Database name
$tbl_name="roles"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$userid", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$userid=$_POST['userid'];
$password=$_POST['password'];
$sql="SELECT * FROM $tbl_name WHERE username='$userid' and password='$password'";
$result=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row


if($count==1){
$result=mysql_fetch_array($result);
$roles = $result['roles'];

//page link on the basis of user role you can add more condition on the basis of ur roles in db
if($roles =='admin'){
$link = 'admin/index.html';
}
elseif($roles =='management'){
$link = 'management/index.html';
}
else $link = 'student/index.html';
}
else {
echo "Wrong Username or Password";
}
?>
[/CODE]

Harap sifu kat sini buleh bantu aku.

Sekian. Edited by M Zainal Abidin Mamat

Share this post


Link to post
Share on other sites
[CODE]
if($count==1){
$result=mysql_fetch_array($result);
$roles = $result['roles'];

//page link on the basis of user role you can add more condition on the basis of ur roles in db
if($roles =='admin'){
$link = 'admin/index.html';
}
elseif($roles =='management'){
$link = 'management/index.html';
}
else $link = 'student/index.html';
}
else {
echo "Wrong Username or Password";
}
?>
[/CODE]

Hmm aku tak expert. Tapi kenapa tak guna function header ek? Or ada planning lain lepas coding atas nie?

Why not guna macam ni?

[CODE]
if($count==1){
$result=mysql_fetch_array($result);
$roles = $result['roles'];

//page link on the basis of user role you can add more condition on the basis of ur roles in db
if($roles =='admin'){
header('Location: admin/index.html');
}
elseif($roles =='management'){
header('Location: management/index.html');
}
else{
header('Location:student/index.html');
}
else {
header('Location: error.html');
}
?>
[/CODE]

[url="http://php.net/manual/en/function.header.php"]http://php.net/manual/en/function.header.php[/url]

Share this post


Link to post
Share on other sites
Aku rasa yang tak jalan tu sebab takde redirect lepas condition tu. Selain header boleh jugak guna META (html)


[CODE]<META HTTP-EQUIV="Refresh" CONTENT="2; URL=admin/index.html">[/CODE]

2 = 2 saat delay sebelum ke page yang kita set kan tu

kipidap~

Share this post


Link to post
Share on other sites
sile asing kan file db connection ke file lain...
takut org leh parse and tau apa db password...

lagi 1

while($row = mysql_fetch_array($result))
{

$roles = $row['roles'];

}

if($roles)
{

session_start();
session_register('username');
session_register('roles');
header('location: index.php')

}



setiap header page

session_start()

$roles = $_session['roles'];


if($roles == "admin")
{
echo "welcome admin";

}
else if ($roles == "staff")
{
echo "welcome staff";

}
else if ($roles == "student")
{
echo "welcome student";

}
else
{
header('location: logout.php');

}



see above and understand it.....
its worth it...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...