By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

php session variable

P: n/a
Hi everyone,
I am stuck on something really stupid and simple. Here is what I am
trying to do. Login, check the username and password against db and if
it checks out then check the logged variable session and pass the user
to the next page. Here is a sample of the login process page:

$user = 'test';
$pass = sha1('testpass');
//set the database connection variables
$hostname = "localhost";
$dbusername = "newuser";
$dbpassword = "newpassword";
$dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
die("Unable to connect to the database");
mysql_select_db("seandb", $dbh);
$result = mysql_query("select * from users where username='$user' AND
password='$pass'", $dbh);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){

//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}
}else {
include 'index.php';
printf("Incorrect Login...");
}

Then on the Main page I want to be able check the variable sname and
if it is set then say hi.

For some reason this doesn't happen. The variable gets assigned a
variable but once it is passed over to main.php, the session variable
is not there anymore. Here is the code for main.php:
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p><?php echo($_SESSION['sname']);?>

<?php
}else{
printf("failed...");
//header( "Location: index.php" );
}

For some reason I always get the "failed" message

any ideas...?

Thanks

J

Jul 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
Hi everyone,
I am stuck on something really stupid and simple. Here is what I am
trying to do. Login, check the username and password against db and if
it checks out then check the logged variable session and pass the user
to the next page. Here is a sample of the login process page:

$user = 'test';
$pass = sha1('testpass');
//set the database connection variables
$hostname = "localhost";
$dbusername = "newuser";
$dbpassword = "newpassword";
$dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
die("Unable to connect to the database");
mysql_select_db("seandb", $dbh);
$result = mysql_query("select * from users where username='$user' AND
password='$pass'", $dbh);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){

//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}}else {

include 'index.php';
printf("Incorrect Login...");

}

Then on the Main page I want to be able check the variable sname and
if it is set then say hi.

For some reason this doesn't happen. The variable gets assigned a
variable but once it is passed over to main.php, the session variable
is not there anymore. Here is the code for main.php:
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p><?php echo($_SESSION['sname']);?>

<?php}else{

printf("failed...");
//header( "Location: index.php" );

}

For some reason I always get the "failed" message

any ideas...?

Thanks

J
Two things:

1. Don't use session_register. Just put it in the session array:
$_SESSION['sname'] = $user;

2. On main.php are you calling session_start() before you do
anything? If you aren't, you should be.

Jul 3 '07 #2

P: n/a
On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
Hi everyone,
I am stuck on something really stupid and simple. Here is what I am
trying to do. Login, check the username and password against db and if
it checks out then check the logged variable session and pass the user
to the next page. Here is a sample of the login process page:
$user = 'test';
$pass = sha1('testpass');
//set the database connection variables
$hostname = "localhost";
$dbusername = "newuser";
$dbpassword = "newpassword";
$dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
die("Unable to connect to the database");
mysql_select_db("seandb", $dbh);
$result = mysql_query("select * from users where username='$user' AND
password='$pass'", $dbh);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){
//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}}else {
include 'index.php';
printf("Incorrect Login...");
}
Then on the Main page I want to be able check the variable sname and
if it is set then say hi.
For some reason this doesn't happen. The variable gets assigned a
variable but once it is passed over to main.php, the session variable
is not there anymore. Here is the code for main.php:
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p><?php echo($_SESSION['sname']);?>
<?php}else{
printf("failed...");
//header( "Location: index.php" );
}
For some reason I always get the "failed" message
any ideas...?
Thanks
J

Two things:

1. Don't use session_register. Just put it in the session array:
$_SESSION['sname'] = $user;

2. On main.php are you calling session_start() before you do
anything? If you aren't, you should be.
I made the changes but still no cigar.
login.php
---------
while($row = mysql_fetch_array($result)){

//start the session
session_start();
$_SESSION['sname'] = $user;
//successful login code will go here...
header( "Location: main.php" );
}
main.php
---------
session_start();
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p>

<?php
}
?>

Jul 3 '07 #3

P: n/a
On Jul 3, 6:17 pm, Jack <accpac...@hotmail.comwrote:
On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
Hi everyone,
I am stuck on something really stupid and simple. Here is what I am
trying to do. Login, check the username and password against db and if
it checks out then check the logged variable session and pass the user
to the next page. Here is a sample of the login process page:
$user = 'test';
$pass = sha1('testpass');
//set the database connection variables
$hostname = "localhost";
$dbusername = "newuser";
$dbpassword = "newpassword";
$dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
die("Unable to connect to the database");
mysql_select_db("seandb", $dbh);
$result = mysql_query("select * from users where username='$user' AND
password='$pass'", $dbh);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){
//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}}else {
include 'index.php';
printf("Incorrect Login...");
}
Then on the Main page I want to be able check the variable sname and
if it is set then say hi.
For some reason this doesn't happen. The variable gets assigned a
variable but once it is passed over to main.php, the session variable
is not there anymore. Here is the code for main.php:
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p><?php echo($_SESSION['sname']);?>
<?php}else{
printf("failed...");
//header( "Location: index.php" );
}
For some reason I always get the "failed" message
any ideas...?
Thanks
J
Two things:
1. Don't use session_register. Just put it in the session array:
$_SESSION['sname'] = $user;
2. On main.php are you calling session_start() before you do
anything? If you aren't, you should be.

I made the changes but still no cigar.
login.php
---------
while($row = mysql_fetch_array($result)){

//start the session
session_start();
$_SESSION['sname'] = $user;
//successful login code will go here...
header( "Location: main.php" );
}
main.php
---------
session_start();
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p>

<?php}

?>
Make sure you have cookies enabled. Also make sure error_reporting
and display_errors is enabled so you can see if there are any problems.

Jul 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.