Jump to content

Blank screen on successful login


strawbshaker

Recommended Posts

Hi everyone,

I am having trouble with a snipet of login code that I am working on. most of it seems to be working fine as all the following work:
- The login failed screen comes up when the username and password are entered incorrectly and don't match what is held in the database.
- When login is successful the database is updated accordingly with the date/time of last login.

What doesn't happen though is the login successful screen appearing. I think it might have something to do with the following line of code:
[code]header("Location: /loggedin.php");[/code]

Can anyone help? The full code for my login.php is below:

[code]
<?php
include $_SERVER['DOCUMENT_ROOT'].'/common.php';
include $_SERVER['DOCUMENT_ROOT'].'/layout.php';

$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];
switch($req){
   
case "validate":
   $validate = mysql_query("SELECT * FROM dbtable
                           WHERE username='{$_POST['username']}'
                           AND password = md5('{$_POST['password']}')
                           AND verified='1'
                           ") or die (mysql_error());
                           
   if(mysql_num_rows($validate) == 1){
      while($row = mysql_fetch_assoc($validate)){
         $_SESSION['login'] = true;
         $_SESSION['userid'] = $row['id'];
         $_SESSION['first_name'] = $row['first_name'];
         $_SESSION['last_name']  = $row['last_name'];
         $_SESSION['email_address'] = $row['email_address'];
         setcookie('userid', $unique_userid, time()+24*3600*60);
         
         if($row['admin_access'] == 1){
            $_SESSION['admin'] = true;
         }
         $login_time = mysql_query("UPDATE dbtable
                       SET last_login=now()
                       WHERE id='{$row['id']}'");
       }
       session_write_close();
       header("Location: http://www.mydomain.com/loggedin.php");
   } else {
      myheader("Login Failed!");
      echo '<p align="center">Login Failed</p>';
      echo '<p align="center">Your user name or password '.
           'is incorrect. Passwords are case sensitive. '.
           'Please check and try again.';
      myfooter();
   }
break;

default:
   myheader("Login!");
      include $_SERVER['DOCUMENT_ROOT'].
              '/html/forms/login_form.htm';
   myfooter();
break;
}

?>
[/code]
Link to comment
Share on other sites

I have tried inserting the following as suggested:
[code]$login_time = mysql_query("UPDATE dbtable
                      SET last_login=now()
                      WHERE id='{$row['id']}'");
      }
      session_write_close();
      header("Location: /loggedin.php");[/code]

I have tried using set cookie recommended in anther thread and that hasn't worked either.
Link to comment
Share on other sites

Try this when you login should goto google ok.
[code]
<?php
include $_SERVER['DOCUMENT_ROOT'].'/common.php';
include $_SERVER['DOCUMENT_ROOT'].'/layout.php';

$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];
switch($req){
 
case "validate":
  $validate = mysql_query("SELECT * FROM dbtable
                          WHERE username='{$_POST['username']}'
                          AND password = md5('{$_POST['password']}')
                          AND verified='1'
                          ") or die (mysql_error());
                         
  if(mysql_num_rows($validate) == 1){
      while($row = mysql_fetch_assoc($validate)){
        $_SESSION['login'] = true;
        $_SESSION['userid'] = $row['id'];
        $_SESSION['first_name'] = $row['first_name'];
        $_SESSION['last_name']  = $row['last_name'];
        $_SESSION['email_address'] = $row['email_address'];
       
        if($row['admin_access'] == 1){
            $_SESSION['admin'] = true;
        }
        $login_time = mysql_query("UPDATE dbtable
                      SET last_login=now()
                      WHERE id='{$row['id']}'");
     
      header("Location: http://www.google.com");
}
  } else {
      myheader("Login Failed!");
      echo '<p align="center">Login Failed</p>';
      echo '<p align="center">Your user name or password '.
          'is incorrect. Passwords are case sensitive. '.
          'Please check and try again.';
      myfooter();
  }
break;

default:
  myheader("Login!");
      include $_SERVER['DOCUMENT_ROOT'].
              '/html/forms/login_form.htm';
  myfooter();
break;
}

?>
[/code]
Link to comment
Share on other sites

I have added/updated the following code in my mai script and still no joy:

[code]setcookie('userid', $unique_userid, time()+24*3600*60);[/code]

[code]session_write_close();
header("Location: http://www.mydomain.com/loggedin.php");[/code]

Any other advise? Thank you to those who have already contributed.
Link to comment
Share on other sites

Try putting this code:

[code]<?php
    echo "made it here!";
    exit; //no more code executed
?>[/code]at the top of your 'loggedin.php' page.  Maybe that page has an error in it.  Maybe a syntax error, try calling the page directly from the address field of your browser with the code above in it.
Link to comment
Share on other sites

Can you post the login form?  I'm used to login forms sending $_POST variables.  Could that be the problem, or does your login form process itself and then send the user to this validation page with the  POST variables as REQUEST variables?

Though, if this is the case it should still go to default, but it seems like it's worth a shot.
Link to comment
Share on other sites

I have moved the header() as suggested by redarrow and changed to content of my loggedin.php fole to check if the error is there. I have combed through every possible connecting script to check for errors and I can't find any thing wrong. I feel like crying as I got stuck on this when I went to bed last night and have been working on it all day so far and haven't got any where. AAAGGGHHH!!!

Thanks for trying to help though and keep the suggestions coming! i really appreciate any help.
Link to comment
Share on other sites

here's the login_form.htm:

[code]<div align="center">Please Login</div>

<form action="/login.php" method="post">
  <table width="30%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="19%">Username:</td>
      <td width="81%"><input name="username" type="text" id="username" value="<?=$_POST['username'];?>" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input name="password" type="password" id="password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><div align="center">
          <input type="hidden" name="req" value="validate" />
          <input type="submit" name="Submit" value="Submit" />
        </div></td>
    </tr>
  </table>
</form>
[/code]
Link to comment
Share on other sites

Correct me if I'm wrong, but post variables go into the $_POST array, so you should change the line:

[code]
$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];
[/code]

to:

[code=php]
$req = (!isset($_POST['req'])) ? 'default' : $_POST['req'];
[/code]
Link to comment
Share on other sites

[code]
<div align="center">Please Login</div>

<form action="/loggedin.php" method="POST">
  <table width="30%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="19%">Username:</td>
      <td width="81%"><input name="username" type="text" id="username" value="<?=$_POST['username'];?>" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input name="password" type="password" id="password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><div align="center">
          <input type="hidden" name="req" value="validate" />
          <input type="submit" name="Submit" value="Submit" />
        </div></td>
    </tr>
  </table>
</form>

[/code]
Link to comment
Share on other sites

session_start(); is in the one of the includes. The code used looks like this:

[code]session_start();
session_name('ClientLogin');
header("Cache-control: private"); //Fix for IE[/code]

I have tried replacing $_REQUEST with $_POST and I am still getting the same result. Should the form method for the form in login_form.htm not be $_POST instead of just POST maybe?
Link to comment
Share on other sites

[color=red][size=18pt]REDARROW IS INDEED A PHP GURU! [/size] [/color]

A true superstar! Thanks dude. Changing the form action to loggedin.php from login.php solved the problem. I can't believe I have spent ALL DAY (from 10am to 18:16pm UK time) trying to solve this silly mistake. Thanks again. I'm not sure I would ever have thought to revisit that part of my code.

Thanks to everyone else for helping, you're all true gems!
Link to comment
Share on other sites

[color=blue][b]Last question:[/b][/color]

Is it possible to have the form action set to 2 pages?

For example:
[code]<form action="login.php,loggedin.php" method="POST">[/code]

The reason I want to do this is because:
a) when it is set to login.php it updates things like the hyperlink box to display the right section of hyper link but then I get the blank screen on successful login.
b) when it is set to loggedin.php I get to the page I want to be at but it doesn't update the hyperlinks box.

Any advice?

Link to comment
Share on other sites

from your login result send them to a update page to update the information then send then to the home page ok.

on login_result.php add this so the user goes to update and back agin ok.

you add this on the page that the form post to ok.
[code]

if($_GET['redirect']=="user"){
header("location: update.php?cmd=update");
}
[/code]


add this to the form already done ok.
[code]
<div align="center">Please Login</div>

<form action="/loggedin.php?redirect=user" method="POST">
  <table width="30%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="19%">Username:</td>
      <td width="81%"><input name="username" type="text" id="username" value="<?=$_POST['username'];?>" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input name="password" type="password" id="password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><div align="center">
          <input type="hidden" name="req" value="validate" />
          <input type="submit" name="Submit" value="Submit" />
        </div></td>
    </tr>
  </table>
</form>
[/code]

fill in what needs to be filled in ok.

copy and past call it update.php

update.php

[code]

<?php session_start();

// database connection.

$db=mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxxxxxxxx",$db);


// from a link if cmd == update

if($_GET['cmd']=="update"){


$query_update="update xxx set xxx=xxx where xxx=xxx and xxx=xxx ";
$result_query=mysql_query($query_update);

header("location: index.php");

}
?>

[/code]

i hope you can work on this ok good luck.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.