PDA

View Full Version : php session not working in frame ... need help



pepe_lepew1962
11-28-2008, 08:22 PM
Hello:

When I run my php file via index00 in the frame02, the password screen checks password and if successful (which I know it is right ) for some reason jumps back to the same login screen but blank. When I make a typo or mistake, then the failed message appears properly in the correct frame02. To check if everything is okay I run the php file main_login.php in my web browser and everything works. If I then run my index00 file with the correct id and password it suddenly works. I can only guess that it has something to do with the sessions, but my background is shallow. I have provided all the source code in hopes that someone can help me with this.


***** checklogin.php *****
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="****"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // 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");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect 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);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location://127.0.0.1/login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

***** main_login.php *****
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="http://127.0.0.1/checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member 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>

***** login_success.php *****
<?php
session_start();
if(!session_is_registered(myusername)){
header("location://127.0.0.1/main_login.php");
}
// Check if session is not registered , redirect back to main page.
// Put this code in first line of web page.
?>

***** loginout.php *****
<?php
session_start();
session_destroy();
?>

***** index00.htm *****
<html>
<body>
Login Successful
</body>
</html>


<html>

<head>
<title>New Page 1</title>
</head>

<frameset rows="64,*">
<frame name="contents1" src="index01.htm"scrolling="no" noresize target="contents5">
<frameset cols="150,*">
<frameset rows="*,48%,26%">
<frame name="contents2" src="http://127.0.0.1/main_login.php" target="contents5">
<frame name="contents3" src="index03.htm" target="contents5">
<frame name="contents4" src="index04.htm" target="contents5">
</frameset>
<frame name="contents5" src="index05.htm">
</frameset>
<noframes>
<body>

<p>This page uses frames, but your browser doesn't support them.</p>

</body>
</noframes>
</frameset>

</html>

***** index01.htm *****
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Center</title>
</head>

<body>

<table border="1" width="100%" id="table1">
<tr>
<td>
<p align="center">Center</td>
</tr>
</table>

</body>

</html>


***** index03.htm *****
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Link 01</title>
</head>

<body>

<table border="1" width="75%" id="table1">
<tr>
<td>Link 01</td>
</tr>
<tr>
<td>Link 02</td>
</tr>
<tr>
<td>Link 03</td>
</tr>
<tr>
<td>Link 04</td>
</tr>
<tr>
<td>Link 05</td>
</tr>
<tr>
<td>Link 06</td>
</tr>
</table>

</body>

</html>

***** index04.htm *****
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search 01</title>
</head>

<body>

<table border="1" width="75%" id="table1">
<tr>
<td>Search 01</td>
</tr>
<tr>
<td>Search 02</td>
</tr>
<tr>
<td>Search 03</td>
</tr>
<tr>
<td>Search 04</td>
</tr>
</table>

</body>

</html>


***** index05.htm *****
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Main L-01</title>
</head>

<body>

<table border="1" width="100%" id="table1">
<tr>
<td>Main L-01</td>
<td>Main R-01</td>
</tr>
<tr>
<td>Main L-02</td>
<td>Main R-02</td>
</tr>
<tr>
<td>Main L-03</td>
<td>Main R-03</td>
</tr>
<tr>
<td>Main L-04</td>
<td>Main R-04</td>
</tr>
<tr>
<td>Main L-05</td>
<td>Main R-05</td>
</tr>
<tr>
<td>Main L-06</td>
<td>Main R-06</td>
</tr>
<tr>
<td>Main L-07</td>
<td>Main R-07</td>
</tr>
<tr>
<td>Main L-08</td>
<td>Main R-08</td>
</tr>
<tr>
<td>Main L-09</td>
<td>Main R-09</td>
</tr>
<tr>
<td>Main L-10</td>
<td>Main R-10</td>
</tr>
</table>

</body>

</html>




***** createsdb.txt *****
use test;
CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

--
-- Dumping data for table `members`
--

INSERT INTO `members` VALUES (1, 'john', '1234');