Link to home
Start Free TrialLog in
Avatar of perlwhite
perlwhiteFlag for United States of America

asked on

Page after login not working

Hello,

I have a simple login page, authorization page and then the page that I want to display.  However, I am not able to view the 3 rd page.  Here is my code.
This is my 3rd page which is not displaying....
<?php
session_start();

if($_SESSION['logged']){
   
echo "logged";



<html>
<head>
      <title>Test123</title>
      <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Karla:400,700">
      <link rel="stylesheet" href="css/screen.css" media="screen"/>
      <link rel="stylesheet" href="css/lightbox.css" media="screen"/>
</head>

//some other html stuff
}
?>
*******************************************
Page2- validate_login.php
<?php
ob_start();
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="*******"; // Mysql password
$db_name="abc"; // Database name
$tbl_name="abcd"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);


$count=mysql_num_rows($result);



if($count==1){
 session_start();
$_SESSION['logged'] = TRUE;
session_register("myusername");
session_register("mypassword");
header("Location: test.php");

exit;
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
************************************************************************
page1/login.php
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="validate_login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Please Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I don't think I can debug your code, but I can show you the design pattern, which has tested and working code, in this article:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
Avatar of perlwhite

ASKER

OK,  can you give me any hints why I am not able to redirect to the 3rd page.  After login button is pressed, the control goes to the validate_login page and does not move from there.
Avatar of Marco Gasi
I am not able to view the 3 rd page
What does this mean? You see 'Wrong username or password'? Or a blank page?

Anyway, delete the exit after the call of header: this could fix the issue.
I am not able to view the 3 rd page

This means that the page that is supposed to display after validate_login.php is blank.

Anyway, delete the exit after the call of header: this could fix the issue.

Nope, still getting blank page and the header address bar still says- validate_login.php
Please post a link to the failing web page, and give us any other information we can use to test, thanks.

Also, please read the article on PHP client authentication.  You're not the first one to try this, and there is a wealth of deep background experience built into the article.  From the code I see in this question, it would be easier for you to start over with a stable platform, rather than try to keep building on this one.

If you're new to PHP, this other article can help you get started on a good footing.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not sure if it's a copy/paste error, but you are missing the closing bracket and closing php tag for your if statement on 3rd page:

<?php
session_start();

if($_SESSION['logged']){
    
echo "logged";
}
?>

Open in new window

Thanks experts for your comments. What is confusing is the same exact code works on my localhost but not on my production site.
dimmergeek,

Yes I have that in my code.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you set error_reporting t E_ALL as I suggested in my comment?
If yes, do you receive any message?
If yes, post it here, please.
If not, do it :)
I deleted these and it works now, do not know why!

session_register("myusername");
session_register("mypassword");

but this line works -$_SESSION['logged'] = TRUE;
@Perlwhite: What was wrong with the responses here?  You didn't answer my questions.  You obviously did not read the online man pages, or you would have known that using session_register() was a serious mistake.  You gave the worst grade anyone can give to a colleague here at Experts-Exchange and you did so without any explanation.

Please explain, thanks.  To assist you, here are the grading guidelines:
http://support.experts-exchange.com/customer/portal/articles/481419
Totally agree with Ray: explain why or change grade, please.