2012-07-21 166 views
0

這是我的login.php顯示登錄後的用戶名和用戶類型。 PHP

<form action="index.php" method=get> 


     <?php 
    error_reporting(E_ALL & ~E_NOTICE); 
     ?> 


    <?php 
    session_start(); 
    if($_SESSION["logging"]&& $_SESSION["logged"]) 
    { 

    printme(); } 



    else { 
    if(!$_SESSION["logging"]) 
    { 
    $_SESSION["logging"]=true; 

    loginform(); 
    } 
    else if($_SESSION["logging"]) 
    { 
    $number_of_rows=checkpass(); 
    if($number_of_rows==1) 
    {  
    $_SESSION[user]=$_GET[userlogin]; 

    $_SESSION[logged]=true; 

    echo "<h1>You have logged in successfully</h1><br/>"; 
    echo "<a href='logout.php'>Logout</a> | <a href='users.php'>Click to proceed</a>"; 

          } 
           else { 



          loginform(); 
          } 
         } 
        } 








    function loginform() 
    { 

    print ("<center><div id='login_header'><b><font face='Arial Black' color='black' size='4px'>Sign in to Minquep!</font></b></div></cen     ter>"); 
    print("<br/><br/>"); 
    print ("<center><label>Username:</label><input type='text' name='userlogin' size='20'><br/><label>Password:</label><input type='    password' name='password' size='20'></center>"); 
    print "<br/><input type='submit' value='Submit' name='submit' class='submit'>"; 


    } 


    function checkpass() 
    { 

    $dbHost = 'localhost'; 
    $dbUser = 'root'; 
    $dbPass = ''; 
    $dbname = 'minquep_test'; 

    $conn = mysql_connect($dbHost,$dbUser,$dbPass); // Connection Code 
    mysql_select_db($dbname,$conn); // Connects to database 



    $sql = "select * from users where login='$_GET[userlogin]' and password='$_GET[password]'"; 
    $result = mysql_query($sql,$conn) or die(mysql_error()); 
    $fetched = mysql_fetch_array($result); 

    if ($fetched['user_type'] == "moderator"){ 
    echo '<script type="text/javascript">window.alert("You have logged in successfully!\n")</script>'; 
    echo "Welcome {$_SESSION['user']}"; 
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/moderator.php\">"; 
        } 
    if ($fetched['user_type'] == "agent"){ 
    echo '<script type="text/javascript">window.alert("You have logged in successfully!\n")</script>'; 
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/agent.php\">"; 


        } 



        } 



     function content(){ 
     print("<b><h1>hi mr.$_SESSION[user]</h1>"); 
     print "<br><h2>only a logged in user can see this</h2>"; 



     } 

     function printme(){ 
     echo '<script type="text/javascript">window.alert("You have logged in successfully!\n")</script>'; 
     } 

     ?> 




       </form> 

現在,只要在......如果他是USER_TYPE「主持人」,他將被重定向到moderator.php

,如果用戶登錄他的USER_TYPE是「代理「他將被重定向到agent.php

我想要發生的是在用戶將被重定向到的頁面中輸出用戶名和usertype。

這是我在agent.php已經有了和moderator.php

<?php session_start(); 

    echo "Welcome {$_SESSION['user']} . And You are Logged in as /*USER TYPE SHOULD BE DISPLAYED HERE */ "; 

?> 

我得到這個錯誤:

Screenshot of error

+0

我甚至不能指向特定的問題,因爲所有的代碼is sooooooooo wrong – zerkms 2012-07-21 12:27:49

+0

檢查這一行'$ _SESSION [user] = $ _ GET [userlogin];'你沒有引用'user'常量 – codingbiz 2012-07-21 12:27:57

+0

@codingbiz:它不會改變任何東西(我並不是爭辯說報價應該放在那裏,但它不會改變該行的行爲) – zerkms 2012-07-21 12:28:51

回答

0

試試這個在您的agent.php

<?php session_start(); 

    if (array_key_exists('user', $_SESSION) && !empty($_SESSION['user'])) { 

     echo "Welcome {$_SESSION['user']} . And You are Logged in as /*USER TYPE SHOULD BE DISPLAYED HERE */ "; 

    } else { 

     echo "Welcome stranger"; 
    } 

?> 
+0

無法正常工作。即使我以主持人或代理身份登錄,它總是會說陌生人。 我在LOGOUT做正確的事情嗎? <?php session_start(); session_destroy(); header(「location:../login.php」); ?> – 2012-07-21 12:41:59

+0

在agent.php中做一個var_dump($ _ SESSION),它打印什麼?如果它是空的,你的會話處理是完全損壞的 – 2012-07-21 12:43:24

+0

我怎樣才能解決這個問題使用var轉儲?應該去在logout.php或agent.php? – 2012-07-21 12:45:22

0

既然你有不同的PHP文件,moderator.phpagent.php,你可以簡單地使用;

在moderator.php,

echo "Welcome {$_SESSION['user']} . And You are Logged in as MODERATOR"; 

和agent.php,

echo "Welcome {$_SESSION['user']} . And You are Logged in as AGENT"; 

沒有大的任務..

+0

,但它仍然不能識別索引用戶...說錯誤 – 2012-07-21 12:42:44

+0

它說undefined:索引用戶 – 2012-07-21 12:52:31