
|
View Full Version : PHP Session Question
Falco1199 11-22-2002, 04:22 PM OK.. I know I have to have session_start() at the top of the page. But, do I also have to have my session_register(..)'s at the tope of the page? I have a session start at the top, but my session registers are lower, after some MySQL queries and part of the layout, and they're getting a PHP parse error. What should I change? Can they be below the MySQL queries, but above the layout? Or should this not even be a problem?
boostar 11-22-2002, 04:46 PM ************
login.php
session_start();
session_register("info to register");
loggedin.php
session_start();
sasha 11-22-2002, 04:52 PM i bet you are registering stuff line this
session_register($var , $var, $v);
rather then this
session_register("var" , "va" , "v" );
Noldar 11-22-2002, 05:35 PM session_register() can be below the MySQL queries and it should work. I've done this before and haven't had any problems. If your getting parse errors it's probably something like what sasha said.
Richard
ghost 11-22-2002, 06:38 PM You can use session_register() command wherever in you PHP.
And be sure your not using $ in session_register.
Ex.
$data = "Whatever you want";
session_register("data");
kunal 11-23-2002, 06:46 AM Hey,
the parser errors do not have anything to do with the location of your session_register syntax.. the problem is with the syntax on the line the parser error occurs at....
kunal
matt2kjones 11-23-2002, 11:10 AM OK let me explain breifly how sessions work
you need session_start(); at the top of pages ur using the session vars.
then, you can use session_register(); wherever in the script you need it. for example, this below, is a perfectly legal script :
page1.php
<?php
session_start();
$my_var_1 = hello;
$my_var_2 = howdy;
session_register("my_var_1", "my_var_2");
?>
page2.php
<?php
session_start();
$my_var_1 = $_SESSION['my_var_1'];
$my_var_2 = $_SESSION['my_var_2'];
echo($my_var_1); //will echo 'hello'
echo($my_var_2); //will echo 'howdy'
?>
hope this helps
Falco1199 11-24-2002, 05:34 PM Hmm.. weird. Here's my script, the error is on line 2:
if (!$passbool) {
session_register('staff');
session_register('name');
session_register('pass'); }
Anybody see the problem? BTW, is "$my_var_2 = $_SESSION['my_var_2'];" really necessary? I thought you could use session variables without having to access them through a script.
matt2kjones 11-24-2002, 06:29 PM when using session_register use double quotes
basically
these guys : ""
Falco1199 11-24-2002, 07:03 PM Funny.. I have the book "PHP Bible," and for ALL of their session examples, they use single quotes. :confused:
Noldar 11-24-2002, 07:29 PM Single quotes work just fine. I'm not sure why you're getting an error. What you've posted looks fine. Maybe you could post all of the code before the error and the exact error message.
If you'd like you could also email it to me and I'll take a look at it. PM me and I'll give you my email address.
Richard
sasha 11-24-2002, 08:17 PM Hmm, I would look for first previous single quote and if it is closed.
Tux-e-do 11-24-2002, 08:31 PM Are you using windows?
jtrovato 11-24-2002, 10:38 PM paste all the code and see why it is happening. You might also be missing a closing } on another loop/if statement. The single quote should work fine for variables.
Also it shouldn't matter if you are using windows or *nix. I use both platforms and didn't need to change the code to make it work in either.
As far as accessing session variables directly, that has to do with a setting in php configuration file. By default it is turned on so you don't need to use $my_var_2 = $_SESSION['my_var_2']; to gain access to the data within the $my_var_2 variable.
I wonder if it has to do with the coding format.
IE..
If (conditon){
do somthing
}
or the correct way lol
if (condtion)
{
do somthing
}
Falco1199 11-25-2002, 01:46 PM Heh.. I'll post the code, but I'll have to edit a LOT, because it's the staff page. Here ya go.. The page is accessed either from a login page with post variables $name and $pass or from a normal page that has session variables containing $staff, $name, and $pass. If it isn't access from any of these pages or the password/name from the login page is incorrect, line 28's if statement will fail, producing the text "ERROR".
<?php include("/path/cookies.php"); ?>
<html>
<head>
<title>CGP Staff Page</title>
<?php $banner = "BA2Staff"; ?>
<META name="robots" data="noindex,nofollow">
<?php include("/path/upperLayout.inc"); ?>
<!-- TEXTAREA BEGIN -->
<?php
include("/path/SQLvariables.php");
$pass = base64_encode($pass);
// MySQL queries here
// They get one value arrays $pass2 and $level
// (corresponding to $pass and $staff)
$pass = base64_decode($pass);
if ($pass == $pass2[0] && $level[0] > 0 && !IsSet($logout)) {
$staff = $level // line 30, b/c erasing messed up the lines
if (!$passbool) { // I now realize that THIS is line 31.. weird
session_register('staff');
session_register('name');
session_register('pass'); }
print("<center><h3>Welcome to the Staff Page!</h3></center>
Staff page stuff
<br />");
} else {
print("ERROR!");
} ?>
<!-- TEXTAREA END -->
<?php include("/path/lowerLayout.php"); ?>
Cookies.php session_start()s and then registers the three variables and defines $passbool as 1 if the three are already defined. I'm noticing an encoding error looking the script over, I think, but that shouldn't generate a line 31 parse error.. weird. Anyway.. it's not particularly clean, so good luck.
sasha 11-25-2002, 01:50 PM $staff = $level // line 30, b/c erasing messed up the lines
$staff = $level ;
:p
Noldar 11-25-2002, 02:19 PM Sneaky little semi-colons :stickout:
|