Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 12th, 2004, 03:09 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default About Session

Hi
I have a question about member area and session.
everything (session) are working and i can send session to the users compter.
my problem is when users logged in to my website, the sign of login change never changed to to logout
i'm useing php and mysql on windows.
I hope somebody can help me
my code is when user logged in is:
//This code is on top of the checking users on my template.
if ($HTTP_SESSION_VARS['valid_user'])
{
   echo '<a href="logout">logout</a>';
}
else
{
  echo '<a href="loggin">login</a>';
}
//thise code is after when i check user for valid pass and username
$HTTP_SESSION_VARS['valid_user'] = $username;

Thank You
Best Regards
mani_he

Reply With Quote
  #2 (permalink)  
Old September 13th, 2004, 11:01 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Help Please

Thank You

Reply With Quote
  #3 (permalink)  
Old September 13th, 2004, 03:18 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Any particular reason that you're using $HTTP_SESSION_VARS instead of the newer $_SESSION equivalent? If your PHP is too old to support $_SESSION, then you really should either upgrade or petition your admin to upgrade.

Having said that if PHP is new enough to support the $_SESSION superglobal, use that instead and your script should work. If PHP is too old and you cannot upgrade, use session_register to create the session variable and $HTTP_SESSION_VARS to check a value.
Code:
//This code is on top of the checking users on my template.
if ($HTTP_SESSION_VARS['valid_user'])
{
   echo '<a href="logout">logout</a>';
}
else
{
  echo '<a href="loggin">login</a>';
}
//this code is after when i check user for valid pass and username
session_register('username');
http://www.php.net/session_register.

If you have a newer version of PHP, all you have to do is use the $_SESSION superglobal. This is the current practice and the best way to go.
Code:
//This code is on top of the checking users on my template.
if ($_SESSION['valid_user'])
{
   echo '<a href="logout">logout</a>';
}
else
{
  echo '<a href="loggin">login</a>';
}
//thise code is after when i check user for valid pass and username
$_SESSION['valid_user'] = $username;
HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #4 (permalink)  
Old September 14th, 2004, 04:25 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks Rich for answering
my php is new and it's 4.3 or something like that and i can use $_SESSION
my problem is the scrpit for log in and log out is on top of the script for checking the user for pass and username you know what my screen website that i have is like that (maybe that would help)
main menu log in help
home
contact us username:
                           password:
                                     login
I hope you can help me to chang that login (on the top)to log out
if ($_SESSION['valid_user'])
{
 echo 'logout';
}
else echo 'login';

if ($HTTP_POST_VARS['username'] && $HTTP_POST_VARS['password'])
{
  $username = $HTTP_POST_VARS['username'];
  $pass = $HTTP_POST_VARS['password'];
  connect to mysql
  connect to database
  $_SESSION['valid_user'] = $username;
}

but you know what the login never changed to logout
But thanks for your help

thank you
best regards
mani_he

Reply With Quote
  #5 (permalink)  
Old September 15th, 2004, 05:36 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

If you log in and the value of $_SESSION['valid_user'] is tested before it is created, then that won't work. That is to say if in your page, the code appears in this order:
Code:
if ($_SESSION['valid_user'])
{
 echo 'logout';
}
else echo 'login';

if ($HTTP_POST_VARS['username'] && $HTTP_POST_VARS['password'])
{
  $username = $HTTP_POST_VARS['username'];
  $pass = $HTTP_POST_VARS['password'];
  connect to mysql
  connect to database
  $_SESSION['valid_user'] = $username;
}
This needs to be the other way around.
Code:
if ($HTTP_POST_VARS['username'] && $HTTP_POST_VARS['password'])
{
  $username = $HTTP_POST_VARS['username'];
  $pass = $HTTP_POST_VARS['password'];
  connect to mysql
  connect to database
  $_SESSION['valid_user'] = $username;
}


if ($_SESSION['valid_user'])
{
 echo 'logout';
}
else echo 'login';
You really should use the short superglobal names for all variables. $_POST instead of $HTTP_POST_VARS, $_GET instead of $HTTP_GET_VARS.

HTH!


Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #6 (permalink)  
Old September 16th, 2004, 10:36 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks Rich
It's work the way that you told me, thanks
and I will use short suerglobal
Thank You
Best Regards
mani_he

Reply With Quote
  #7 (permalink)  
Old September 16th, 2004, 11:37 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi Rich
I got another problem
on the page of login, the login sign changed to logout when user put the valid name and pass but when they go to the other page of my website the logout changed to login and that means $_SESSION could not read the session on the other page
i used this script for all pages
if ($_SESSION['valid_user'])
{
 echo 'logout';
}
else echo 'login';

I hope you can help me
thank you


Reply With Quote
  #8 (permalink)  
Old September 18th, 2004, 03:47 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi rich
about my last post that means that session does not work for all pages, it only changes login sign to logout in login.php
but when the user goes to home.php the sign never changed.

Hope for help
Thank You
Best Regards

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
session komalpriya .NET Framework 2.0 4 October 30th, 2007 09:16 AM
session lakshmi devi Classic ASP Basics 4 July 20th, 2006 04:33 AM
session and cookie problem (empty session file) msincan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 27th, 2005 05:31 PM
session help -Dman100- Classic ASP Basics 1 November 29th, 2004 12:45 AM
Session quinn Classic ASP Basics 12 November 19th, 2003 10:42 AM



All times are GMT -4. The time now is 08:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.