当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Output the username and user type after Logged In Session PHP

问题描述:

Ok im stuck with this. I tried the isset function but nothing happens...

Once logged in, the user will be redirected to a specific page.

If moderator is her user_type then she'll be redirected to moderator.php page

If agent is her user_type she'll be redirected to agent.php

I've here the index.php where login form is included

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

<?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>';

print("<b><h1>hi mr.$_SESSION[user]</h1>");

echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/moderator.php\">";

}

else 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>

From that code above this is how I redirect users to their specific pages based on their user_type.

if ($fetched['user_type'] == "moderator") {

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

print("<b><h1>hi mr.$_SESSION[user]</h1>");

echo "<meta http-equiv=\"refresh\" content=\"0;URL=pages/moderator.php\">";

}

else 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\">";

}

Now inside my moderator.php

I just call the moderator_include.php where I supposed to print the username and user_type of the logged in user.

moderator.php

<div id="wrapper">

<div id="container">

<div id="header">

<?php include "moderator_header.php"; ?>

</div>

It includes the moderator_header.php which is

<div class="logo">

<a href="moderator.php"><img class="logo_img" src="../images/minquepLOGO.png"/></a>

</div>

<div id="title">

<img src="../images/title.gif"/>

</div>

<br/>

<?php

session_start();

if ($_SESSION["logged"] = true) {

print("<b><h1>hi mr. $_SESSION[user] . You are logged in as /*THE USER_TYPE GOES HERE */ </h1>");

}

?>

I tried to output the username as

if (isset($_SESSION['logged'])){

print("<b><h1>hi mr. $_SESSION[user] . You are logged in as /*THE USER_TYPE GOES HERE */ </h1>"); }

But nothing happens...

About how to output the user_type of the user... I dont have any idea how to this because it's not a part of session happened in index.php

Btw my logout.php is like this

<?php

session_start();

if (session_destroy()) {

print"<h2><B><blink>you have logged out successfully</B></blink></h2>";

print "<h3><a href='index.php'>back to main page</a></h3>";

}

?>

please help me...thanks

网友答案:

sometimes php gets a little tricky ... a few things to remember

1) always start the session before anyoutput, it means at top of your code, before starting the session there shouldnt be even a single blank space or an empty line.

2) when you have a file that starts session, and includes another file, youd dont have to start it again in the file included.

and inorder to trace your session, in any page you want just add this code :

<pre><?php print_r($_SESSION); ?></pre>

and see what the result is.

网友答案:
print("<b><h1>hi mr " . $_SESSION['user'] . "You are logged in as" . $userType . "</h1>"); }

Try it :)

EDIT

edit the variable $userType to what it should be...

分享给朋友:
您可能感兴趣的文章:
随机阅读: