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

Session variables and header("location:xxx");

P: 15
I'm designing a small site and I have this structure in mind:

index.php
---------------
login fields. User enters login info. LoginCheck.php is called.

LoginCheck.php
-----------------------
Login info are POST collected and checked in DB. If successful, session variables are set. Then redirected to index.php using header("location:index.php");

In index.php. I'm checking is the session variable is registered. But it looks like the session variable is lost during redirection using header.

Is there any workaround for this?
Mar 15 '08 #1
Share this Question
Share on Google+
6 Replies


Markus
Expert 5K+
P: 6,050
If we saw your (relevant) code then we might be able to suggest something.
Mar 15 '08 #2

P: 15
If we saw your (relevant) code then we might be able to suggest something.
[php]
$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 "LoginOK.php"
session_register("myusername");
session_register("mypassword");
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}[/php]
Mar 15 '08 #3

Markus
Expert 5K+
P: 6,050
$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 "LoginOK.php"
session_register("myusername");
session_register("mypassword");
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
Ive never used session_register.
Do you start with session_start() on every page?
Mar 15 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Mar 15 '08 #5

P: 15
Ive never used session_register.
Do you start with session_start() on every page?
I have used session_start() only in the login page. I think it is enough to start a session in login page, and not on every page.

You said you have never used session_register. Then how do you work around with session variables? Can you please help me on this?
Mar 15 '08 #6

Markus
Expert 5K+
P: 6,050
I have used session_start() only in the login page. I think it is enough to start a session in login page, and not on every page.

You said you have never used session_register. Then how do you work around with session variables? Can you please help me on this?
You need to use session_start(); on every page!

When i use sessions i do this:
[php]
# login.php
session_start();
# log in script
# if(login == successful)
$_SESSION['logged_in'] = true;
[/php]
[php]
# member.php
session_start(); # MUST BE INCLUDED!
if(isset($_SESSION['logged_in']))
{
# member is logged in
}
else
{
# member isnt logged in
}
[/php]
session_start(); has to be on every page that you are using sessions in.
:)
Mar 15 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.