473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting $_SESSION in PHP 5

4 New Member
I am having problems getting the $_SESSION to set, the $_SESSION will work fine when its first gets the value, however, when I go to another page, the $_SESSION then loses its value. So if any one can take a look at my code and see if there is any problems, that would be good.

Thanks

Here is my code

[PHP]
<?php if (!isset($_SESSI ON))
session_start() ;

$loginFormActio n = $_SERVER['PHP_SELF'];

if (isset($_POST['txtUserName']))
{
mysql_select_db ($database_con, $con);
$LoginRS = mysql_query("SE LECT * FROM User WHERE passwd='".$_POS T['txtPassword']."' AND login='".$_POST['txtUserName']."'" , $con or die(mysql_error ());
$row_loginFound User = mysql_fetch_ass oc($LoginRS);
$loginFoundUser = mysql_num_rows( $LoginRS);

if ($loginFoundUse r > 0)
{
//declare three session variables and assign them
$_SESSION['conUserID'] = $row_loginFound User['UserID'];
$_SESSION['UserEMail'] = $row_loginFound User['Email'];
$_SESSION['UserName'] = $row_loginFound User['FName']." ".$row_loginFou ndUser['LName'];
}//end of if
}//end of if ?>


<form action="<?php echo $loginFormActio n; ?>" method="post" name="frmLogin" id="frmLogin">
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td align="left">
<br/><label>My Business Continuity <br />Plans</label>
</td>
</tr>

<?php //checks if the user log in
if (isset($_SESSIO N['conUserID']))
{
echo "<tr>
<td align=\"left\">
<a href=\"Page2.ph p?UserID=".$_SE SSION['conUserID']."\">Go To Page 2</a>
<br/><br/>
</td>
</tr>
</td></tr>";
}//end of if
else
{
echo "<tr>
<td>
<label>User Name:</label>
</td>
</tr>
<tr>
<td>
<input name=\"txtUserN ame\" type=\"text\" id=\"txtUserNam e\" />
</td>
</tr>
<tr>
<td>
<label>Password :</label>
</td>
</tr>
<tr>
<td>
<input name=\"txtPassw ord\" type=\"password \" id=\"txtPasswor d\" />
</td>
</tr>";
}//end of else?>
</table>[/PHP]
Jan 2 '08 #1
8 3369
shreedhan
52 New Member
I think the problem is here


[PHP]
<?php if (!isset($_SESSI ON))
session_start() ;
[/PHP]

You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

How about trying just
[PHP]session_start() ;[/PHP]

It would start a session in every page.
I think that would help
Jan 3 '08 #2
SpiritBreaker
4 New Member
I have try that suggestion however it is still not working

I think the problem is here

You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

How about trying just
[PHP]session_start() ;[/PHP]

It would start a session in every page.
I think that would help
Jan 3 '08 #3
SpiritBreaker
4 New Member
I am also using PHP version 5.2.4 if that makes any differance
Jan 3 '08 #4
sumeetk
6 New Member
Hi,

I m using PHP Version 5.2.5 and it is working for, and this is what i did in every page i include

[PHP]session_start() ;

if ($_SESSION['username'] == '')
{
include "loginerror.php ";
die;
exit;
break;
}
$username = $_SESSION['username'];[/PHP]

My login.php has this

[PHP]$username = $_POST[username];

$username = strtolower($use rname);
session_start() ;

$_SESSION['username'] = $username;[/PHP]

Hope this helps

Regards,
Sumeet
Jan 3 '08 #5
shreedhan
52 New Member
Have you included
session_start() ;

in another page or not?

You need to start a session in another page as well.
Jan 4 '08 #6
spudse
11 New Member
You can leave the isset() criteria out, just start every page with

Expand|Select|Wrap|Line Numbers
  1.  session_start(); 
You have to make sure that you that there is no character or space or anything outputted before you use the session_start() function (this will output an error also, about output before the headers have been send).

Also make sure that your browser accepts cookies (sessions are actually cookies until you close your browser). I know that Opera has a nice function to browse cookies.
Jan 7 '08 #7
SpiritBreaker
4 New Member
Sorry, for the late reply.

I have found the problem, the server for some reason keeps changing the session ids on each page on the site.
Jan 25 '08 #8
Atli
5,058 Recognized Expert Expert
Hi.

The most likely reason for the server refreshing the ID is that it cannot find your previous session, or that your browser is unable to store the session ID.

Make sure that the user that is running PHP has write privileges on the directory used to store the session data. You can find the location of that directory in your php.ini under "session.save_p ath". Also make sure the "session.use_co okies" directive is set to On.
If your server is using Windows, the "session.save_p ath" directive must be set.
Jan 25 '08 #9

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

Similar topics

2
1856
by: sumdumgaitu | last post by:
I'm using the following code for a login page in PHP. Everything works OK until someone else logs in from the same browser. As best I can tell $mail, $password, and $user_id are not getting updated in the session. I tried doing session_start(); session_destroy();
2
1561
by: skeeterbug | last post by:
i have a program that will display a number of diffferent results depending on the situation. one example of the code looks like <?php if(isset($_SESSION)) { print 'You entered: ' . $_SESSION . NL . NL; } else {
7
11162
by: deko | last post by:
I have a login page that is supposed to redirect the user to his private page after login. But header("Location: $url") does not work after I set the $_SESSION variable - I get "Warning: Cannot modify header information - headers already sent by ... The abbreviated code on the login page looks like this: <?php session_start(); ?>
1
2603
by: zorro | last post by:
Greetings, I want users to select the duration time of their sessions. I'm able to do it by setting the PHPSESSID cookie duration. Is this reliable, or is it not recommended for some reason? This is the test : open browser, close browser, reopen browser if( ! $_COOKIE) // opened browser first time - no session
1
3231
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
5
2634
by: PaowZ | last post by:
Hello There! I'm trying to handle sessions using php5, but having some troubles to design it.. Ok, I have to use "session_set_save_handler()" to override session management but I don't really understand callback functions arguments. According to doc (http://fr3.php.net/manual/fr/function.session-set- save-handler.php french version), open(), close() take not arg, read() takes $id, write() takes $id, $sess_data and so on...
4
1441
by: jodleren | last post by:
Hi! // get user to edit if( ($_POST=="btn_selusr")||($_POST!="") ) { echo "x:".$_SESSION; $username=stripslashes($_POST); echo "y:".$_SESSION; }
4
5675
by: Hugh Oxford | last post by:
I am writing some server to server software that needs to maintain state. When I say server to server I mean that the client is not a browser, it is another PHP server (as if that wasn't obvious). The obvious way to do this is to use the session ID. The problem is, how to first of all generate a session ID and then send back a session ID to reload a session. Perhaps my understanding of sessions is too limited, but I can't see the
6
11970
hgeithus
by: hgeithus | last post by:
Hi. I have a session variable stored within a cookie, and I want this to change when pressing a hyperlink. i.e. I have this variable $_SESSION = 'en_US'; This is set initially if it hasn't been set before using an if statement:
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.