开发者

How can I login to index.php and have slightly different content being displayed in it from when I am not?

Since the question is a bit vague and unclear, I will clarify what I mean in the following sentences.

The main page of my website is called index3.php and in there, there is the appropriate log in form which requires username and password fields for the user to log in.

What I am trying to do but I couldn't manage yet, is on submit to be redirected in index3.php but instead 开发者_StackOverflow中文版of the log-in form to see text saying "Logged-in as:" in the log-in div. In other words replacing the form with Dynamic Text in the same div the form was in previously. Currently what it does is to redirect you to another page called login.php.

here is the php-code of the index3.php

   <?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=($_POST['password']);
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "login.php";
$MM_redirectLoginFailed = "index3.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_ideatedb, $ideatedb);

$LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password='".md5($_POST['password'])."'",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

$LoginRS = mysql_query($LoginRS__query, $ideatedb) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
 $loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;       

if (isset($_SESSION['PrevUrl']) && false) {
  $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>

Any help or direction will be greatly appreciated.


This line determines where you are redirected:

$MM_redirectLoginSuccess = "login.php";

So if you want to go to index3.php, just change login.php to index3.php.

Then, you'll have to alter index3.php to display the signed-in message instead of the login form. As far as I can tell, this happens in a part of the file you haven't supplied, but the basic idea would probably be like this:

<aside>
  <?php if ($_SESSION['MM_Username']): ?>
    hello, you're signed in as <?php echo $_SESSION['MM_Username']; ?>
  <?php else: ?>
    ...existing login form in index3.php...
  <?php endif; ?> 
</aside>

(In case you're confused by the lack of curly braces, I used the alternative syntax for control structures, because it's slightly nicer when writing spaghetti code.)


something like

<?php
   if(isset($_SESSION['user']){
       echo "Logged in as " . $_SESSION['user'];
   }
?>

is that what you mean?


The reason you are redirected is due to thee last few lines here

if ($loginFoundUser) {
 $loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;       

if (isset($_SESSION['PrevUrl']) && false) {
  $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}

If you change it to something like this

<div id="formHolder">
<?php
    if ($loginFoundUser) {
     $loginStrGroup = "";

    if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    echo "You are logged in"; //You can customize this message using the users username
    }
    else { ?>
    You would put your html form in here
    <?php}
    }?>

This is a really crap way of doing this kind of thing. Remember that just because it works doesn't mean you can't or shouldn't improve it :)


switch($_SESSION['seclevel']){
  case "One security level":
    include('oneseclevel.php');
    break;
  case "Other security level":
    include('otherseclevel.php');
    break;
  default:
    include('login.php');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜