Jump to content

problem with sessions


bonzie

Recommended Posts

Hi all,

I try to create a small website with a login, however I don't get the sessions work.
My login is (index.php): (in HTML)
[code]
<p><form method=post action="home.php>">
  <table cellpadding="2" cellspacing="2" align="center" border="0">
    <tr><td colspan="2" align="center">Please Log in here:</td>
    </tr>
    <tr><td align="right"class="logintext">Username:</td>
           <td><input size="15" maxlength="10" name="userName" type="text"/></td>
    </tr>
    <tr><td align="right" class="logintext">Password:</td>
           <td><input size="15" maxlength="10" name="userPassword" type="password"/></td>
    </tr>
    <tr align="center"><td colspan="2" align="center"><input value="login" name="action" type="hidden"/><input value="Log in" name="submit" type="submit"/>                               </td>
    </tr>
  </table>
[/code]

Now I want to create a session when people login, because I want to be able to identify people (via their username) when they browse further.
I have tried several things but nothing worked. Could you help me?
Thnx
Link to comment
Share on other sites

There are tons of tutorials out there explaining these stuff.
Read, and you still cant figure out why some script doesnt work post it here and we'll try to fix it [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Orio.
Link to comment
Share on other sites

I read already some stuff on sessions, however probably I do something wrong.

From my login page people are directed to index2.php.
On this page I have
[code]
<?php
//start session
session_start();
$username = $HTTP_POST_VARS['userName'];
$password = $HTTP_POST_VARS['userPassword'];
$_SESSION['user']=$username;
...
?>
[/code]
Then in the body of my document (HTML) I have a link:
[code]
<body>
<a class="menu" href="recomm.php">Get Recommendation</a>
</body>
[/code]
I tried it like this, on the page "recomm.php" the session variable (_SESSION['user'])was empty...
What do I wrong?
Is it better to use session_id? And if yes how do I have to integrate this in the URL?

thnx

[!--quoteo(post=385906:date=Jun 20 2006, 11:00 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 20 2006, 11:00 AM) [snapback]385906[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There are tons of tutorials out there explaining these stuff.
Read, and you still cant figure out why some script doesnt work post it here and we'll try to fix it [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Orio.
[/quote]
Link to comment
Share on other sites

in recomm.php I have
[code]
<?php

//start session
session_start();
$username=$_SESSION['user'];
[/code]
When I do
[code]
echo $username;
[/code]
I don't get anything.

[!--quoteo(post=385916:date=Jun 20 2006, 11:24 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 20 2006, 11:24 AM) [snapback]385916[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Do you start the sessions on recomm.php? It has to have session_start() in the begining so it can fetch the vars set on previous pages.

Orio.
[/quote]
Link to comment
Share on other sites

Hmmm
Everything seems fine, maybe it's the problem with the form?
Your code says:
$username = $HTTP_POST_VARS['userName'];
$password = $HTTP_POST_VARS['userPassword'];

Are you sure that in the login form the fields are called userName and userPassword ? And the method is post?

Orio.

[b]EDIT-[/b] I just saw you posted the form as well, and it's ok... I have no idea whats wrong!
Link to comment
Share on other sites

is it maybe better to use session ids?

in the index2.php I would then put
[code]
<?php
//start session
session_start();
$id=session_id();
$username = $HTTP_POST_VARS['userName'];
$password = $HTTP_POST_VARS['userPassword'];
$_SESSION['user']=$username;
$id=$username; // because I want the id to be the username
...
?>
[/code]

Is this correct?
And how do I integrate then the id in the URL?

thnx
[!--quoteo(post=385921:date=Jun 20 2006, 11:36 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 20 2006, 11:36 AM) [snapback]385921[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hmmm
Everything seems fine, maybe it's the problem with the form?
Your code says:
$username = $HTTP_POST_VARS['userName'];
$password = $HTTP_POST_VARS['userPassword'];

Are you sure that in the login form the fields are called userName and userPassword ? And the method is post?

Orio.

[b]EDIT-[/b] I just saw you posted the form as well, and it's ok... I have no idea whats wrong!
[/quote]
Link to comment
Share on other sites

I tried with an easy example:
login form:
[code]
<p><form method=post action="home.php>">
  <table cellpadding="2" cellspacing="2" align="center" border="0">
    <tr><td colspan="2" align="center">Please Log in here:</td>
    </tr>
    <tr><td align="right"class="logintext">Username:</td>
           <td><input size="15" maxlength="10" name="userName" type="text"/></td>
    </tr>
    <tr><td align="right" class="logintext">Password:</td>
           <td><input size="15" maxlength="10" name="userPassword" type="password"/></td>
    </tr>
    <tr align="center"><td colspan="2" align="center"><input value="login" name="action" type="hidden"/><input value="Log in" name="submit" type="submit"/>                               </td>
    </tr>
  </table>

[/code]

Now in home.php i put:

[code]
<?php
session_start();
$id=session_id();
$username = $HTTP_POST_VARS['userName'];
?>

In HTML body
<a href="home2.php?<?php $id ?>"> link</a>
[/code]

The id in the URL doesn't work. And as i am a beginner I don't see why.
Can you help?

[!--quoteo(post=385924:date=Jun 20 2006, 11:46 AM:name=bonzie)--][div class=\'quotetop\']QUOTE(bonzie @ Jun 20 2006, 11:46 AM) [snapback]385924[/snapback][/div][div class=\'quotemain\'][!--quotec--]
is it maybe better to use session ids?

in the index2.php I would then put
[code]
<?php
//start session
session_start();
$id=session_id();
$username = $HTTP_POST_VARS['userName'];
$password = $HTTP_POST_VARS['userPassword'];
$_SESSION['user']=$username;
$id=$username; // because I want the id to be the username
...
?>
[/code]

Is this correct?
And how do I integrate then the id in the URL?

thnx
[/quote]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.