Connecting Tech Pros Worldwide Forums | Help | Site Map

php session variable

Jack
Guest
 
Posts: n/a
#1: Jul 3 '07
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


ZeldorBlat
Guest
 
Posts: n/a
#2: Jul 3 '07

re: php session variable


On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
Quote:
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.

Jack
Guest
 
Posts: n/a
#3: Jul 3 '07

re: php session variable


On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
Quote:
On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
>
>
>
Quote:
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:
>
Quote:
$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);
>
Quote:
//check that at least one row was returned
>
Quote:
$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){
>
Quote:
//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}}else {
>
Quote:
include 'index.php';
printf("Incorrect Login...");
>
Quote:
}
>
Quote:
Then on the Main page I want to be able check the variable sname and
if it is set then say hi.
>
Quote:
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']);?>
>
Quote:
<?php}else{
>
Quote:
printf("failed...");
//header( "Location: index.php" );
>
Quote:
}
>
Quote:
For some reason I always get the "failed" message
>
Quote:
any ideas...?
>
Quote:
Thanks
>
Quote:
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
}
?>

ZeldorBlat
Guest
 
Posts: n/a
#4: Jul 4 '07

re: php session variable


On Jul 3, 6:17 pm, Jack <accpac...@hotmail.comwrote:
Quote:
On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
>
>
>
Quote:
On Jul 3, 6:07 pm, Jack <accpac...@hotmail.comwrote:
>
Quote:
Quote:
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:
>
Quote:
Quote:
$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);
>
Quote:
Quote:
//check that at least one row was returned
>
Quote:
Quote:
$rowCheck = mysql_num_rows($result);
if($rowCheck 0){
while($row = mysql_fetch_array($result)){
>
Quote:
Quote:
//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}}else {
>
Quote:
Quote:
include 'index.php';
printf("Incorrect Login...");
>
Quote:
Quote:
}
>
Quote:
Quote:
Then on the Main page I want to be able check the variable sname and
if it is set then say hi.
>
Quote:
Quote:
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']);?>
>
Quote:
Quote:
<?php}else{
>
Quote:
Quote:
printf("failed...");
//header( "Location: index.php" );
>
Quote:
Quote:
}
>
Quote:
Quote:
For some reason I always get the "failed" message
>
Quote:
Quote:
any ideas...?
>
Quote:
Quote:
Thanks
>
Quote:
Quote:
J
>
Quote:
Two things:
>
Quote:
1. Don't use session_register. Just put it in the session array:
$_SESSION['sname'] = $user;
>
Quote:
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.

Closed Thread