Closed Thread Icon

Topic awaiting preservation: Need Idea on how to begin using Session in PHP. (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=23538" title="Pages that link to Topic awaiting preservation: Need Idea on how to begin using Session in PHP. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Need Idea on how to begin using Session in PHP. <span class="small">(Page 1 of 1)</span>\

 
Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-04-2004 17:59

T.W.I.M.C,
I am just new in working with PHP and i need to konw how to use Session. i.e store and retrive varables from the session. this is what i want to do: i am working on a form that spreads across four pages, and i only want to submit everthing in the database only when the last part has being filled. i.e form on the last page. I used a submit button to navigate to the next page. i.e made the action="...." of my form equals to the address of the next page. I want to be able to drop all values on the previous form(s) in the session so that when i get to the last form, i can collect every thing and then submit to my dear MySql database. I kindly ask for some codes as to how to go about this.. i have not being able to read understanding out of the materials i have being comming across so far.. I believe i would do beter with a sample code(s). Help me if you care.

Thanks
-Fikayo

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-04-2004 18:52

Basic PHP Session Tutorial

would be a good place to start.

so long,

->Tyberius Prime

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 10:59

Prime,
I have being to the Fag Basic Session Tutorial. and just to get a feel of how the code works, i executed it but got the following Error:

quote:
Parse error: parse error, unexpected '&', expecting ',' or ')' in C:\dev\php\practice2.php on line 12



what i have on line 12 was:

quote:
if (isset($HTTP_POST_VARS&#91;'user'&#93)...


I dont even have an idea of what the error means.. i hope you can help explain whats hapenning on line 12.. and how to fix the error..

Thanks

-Fikayo

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-05-2004 13:08

Well... Parses errors occur when the parser can't make sense of something anymore.
The actual mistake might be a line or two before - but since it's still valid code up till the error, the line number seems to be off.

Now, looking at the line you pasted, I'd say that your htmlentities for [ and ] should actually be [ and ].

You might also wish to post code with [code] instead of [quote] - that will prevent slimies and stuff from appearing within in.

so long,

->Tyberius Prime

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 16:14

I coppied, pasted and executed the code below:

code:
<html>
<head>
<title>PHP Session Practice </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//this must be before any printing is being done, inside or outside of the php tags.
session_start();

if (isset($HTTP_POST_VARS['user']))
{
if (doUserAndPasswordMatch($HTTP_POST_VARS['user'],$HTTP_POST_VARS['password']))
{
loginUser($HTTP_POST_VARS['user']);
}
}

if ($myUser = getCurrentUser())
{
print 'You are logged in'. $myUser;
}
else
{
print 'You are not logged in.<br>';
printLoginForm();
}

function printLoginForm() //void
{
print "<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">"; //phpself is the complete url of the current file...

print '<input type="text" name="user" value="">';
print '<input type="password" name="password" value="">';
}

function getCurrentUser() //string(username), or False
{
if (isset($_SESSION['username']))
return $_SESSION['username'];
else
return False;
}

function loginUser($user) //:void
{
$_SESSION['username'] = $user;
}


function doUserAndPasswordMatch($user,$password) //:boolean
{
//You'd probably replace that with a database lookup...
return ((stringToLower($user) == "shu") && ($password == "sha"));
}
?>
</body>
</html>



I got the following error:

quote:
Parse error: parse error, unexpected T_STRING in C:\dev\php\practice2.php on line 32



so i change line 32 from:

code:
print "<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">";



To:

code:
print '<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">';



the difference was that i changed the double quote "..." arround the "print" command value to single quote ' ...'.

it still gave me same error until i removed the

code:
{$_SERVER['PHP_SELF']}



i then executed it and got the following result:

1. The following error was at the top of the page:

quote:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\dev\php\practice2.php:8) in C:\dev\php\practice2.php on line 10

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\dev\php\practice2.php:8) in C:\dev\php\practice2.php on line 10



followed by:
2. You are not logged in.
3. and then two textfields.

I couldn't go further, thus decided to post again!

-Fikayo

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-05-2004 16:30


The code is rather clear :

code:
//this must be before any printing is being done, inside or outside of the php tags.
session_start();

It means that the session_start(); statement MUST be before any output. And in your script there is a lot of output before that statement :

code:
<html>
<head>
<title>PHP Session Practice </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>



Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 16:44

Tthink i was just too anxoius to see my result that i did not get the time to read all instruction! its done now!

I still dont know why i cant use

code:
action="{$_SERVER['PHP_SELF']}"


in my forms i keep getting error such as:

quote:
Parse error: parse error, unexpected T_STRING in C:\dev\php\practice2.php on line 34


any idea?

-Fikayo

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-05-2004 17:45

Fikzy: Since you're at it, you should re-read the manual about the print function. You also have some problems with quotes escaping. For instance :

code:
print "<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">";

should be

code:
print "<form method=\"post\" enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\">";

and the problem in

code:
print '<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">';

is that in a string surrounded by single quotes the characters are output(ed) as is. Thus {$_SERVER['PHP_SELF']} is not interpreted and the single quotes in it are f**ing up the string. So long, to avoid that kind of problems you'd better simply use string concatenation and voilà:

code:
echo '<form method="post" enctype="multipart/form-data" action="'. $_SERVER['PHP_SELF'] .'">';

You can use echo or print. That's, in 99% of the cases, just a matter of choice.

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 18:07

Thanks,
Wow! so much to lern in one day! thats done also!
I was going to say, if i do not specify an action for my form in php, the form redirects to itself right?
-Fikayo

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-05-2004 18:25

in theory. I wouldn't bet on it.
I usually use $_SERVER['PHP_SELF'] to redirect here again.

Now, I'm a bit busy right now fikzy, so feel free to ammend and correct that faq tutorial if necessary. I replaced the htmlentities for [] already earlier today, but there seem to be other problems.

Good luck on your further learning,

->Tyberius Prime

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-05-2004 18:26

"if i do not specify an action for my form in php, the form redirects to itself right ?"
Yes.

But in fact, that's a bad practice as the DTDs of HTML 4.01 and XHTML 1.0 specify that the action attribute is REQUIRED.

code:
<!ATTLIST FORM
%attrs; -- %coreattrs, %i18n, %events --
action %URI; #REQUIRED -- server-side form handler --
method (GET|POST) GET -- HTTP method used to submit the form--
enctype %ContentType; "application/x-www-form-urlencoded"
accept %ContentTypes; #IMPLIED -- list of MIME types for file upload --
name CDATA #IMPLIED -- name of form for scripting --
onsubmit %Script; #IMPLIED -- the form was submitted --
onreset %Script; #IMPLIED -- the form was reset --
accept-charset %Charsets; #IMPLIED -- list of supported charsets --
>



Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 18:50

Great! so i'll just use

code:
$_SERVER['PHP_SELF']

any time i want to "redirect here again".
Today has being realy great! I feel like i can Biuld "PhpMyAdmin" Already!

Thanks to All
-Fikzy

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 18:51

Great! so i'll just use

code:
$_SERVER['PHP_SELF']

any time i want to "redirect here again".
Today has being realy great! I feel like i can Biuld "PhpMyAdmin" Already!

whats the difference between using GET or POST anyway?

Thanks to All
-Fikzy

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-05-2004 19:53

"whats the difference between using GET or POST anyway?"
Why don't you simply try the two methods or ask Google to find out by yourself

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 10-05-2004 19:57

that i'll do!

Thanks
-Fikzy

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-05-2004 20:10

yeah, read the HTTP RFC if you really wanna now.

and any bloody fool could build phpMyAdmin, then spent years tinkering with the darn interface.
I just wish the buttons and menus and whatevers where called about the same and in about the same places with the different versions of that bugger. But no... every single system I have to access has a different version, all looking different, and behaving slightly, but not completly, different. ARGH

« BackwardsOnwards »

Show Forum Drop Down Menu