473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

php session variable

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
3 2100
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
1
by: Ann Leland | last post by:
I have been using session variables to pass a user name from one ASP page to another inside framesets for 9 months and it stopped working this week. I have made no code changes but there was a...
2
by: Eric | last post by:
Hi, I've a problem with trying to retrieve a session variable in an include file. Basically, the main asp creates a session variable: <% Session("var1") = "Hello" %> And then when I click...
4
by: VB Programmer | last post by:
If I have a variable I want to share in my application what is the difference between just declaring a variable (Dim strMyVar as String) and using a session variable (Session("strMyVar"))? When...
9
by: William LaMartin | last post by:
I have a problem, mentioned here before, of Session and Application variables disappearing at one site but not at others or on my development computer. The problem is illustrated by an example...
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
4
by: T Ralya | last post by:
I am told that ASP.NET controls the session ID and session variables, but that does not fit my symptoms. I am posting here as directed. I'm hoping that someone can at least recommend something to...
3
by: Alan Wang | last post by:
Hi there, Once my application gets complicated and complicated. I found it's really hard to keep track of Session value I am using in my asp.net application. I am just wondering if anyone have...
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.