...

View Full Version : session of username, Firstname is blank



jarv
05-15-2008, 10:57 AM
<?
session_start();
$_SESSION['username']='$myusername';
if(!session_is_registered(myusername)){
header("location:index.php");
}
?>

<html>
<body>
<form action="insert.php" method="post">
Firstname: <? $myusername?>


checklogin.php page:

<?php
require_once('connection.php');
$tbl_name="members";

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

// 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:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

RMcLeod
05-15-2008, 11:28 AM
add session_start() to the top of your check login page

then change your session_register statements as below. This is because session_register is deprecated as of PHP version 4.1.0



<?php
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
?>


I wouldn't recommend storing a password in a session variable, if you have a page that requires the password again have the user re-input it.

jarv
05-15-2008, 11:53 AM
thanks, that did not work!

RMcLeod
05-15-2008, 12:07 PM
remove $_SESSION['username']='$myusername'; from your login_success page, this is overwriting the value already set on the check login page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum