Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 12th, 2004, 03:09 PM
Friend of Wrox
 
Join Date: Aug 2004
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

 
Old September 13th, 2004, 11:01 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Help Please

Thank You

 
Old September 13th, 2004, 03:18 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old September 14th, 2004, 04:25 PM
Friend of Wrox
 
Join Date: Aug 2004
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

 
Old September 15th, 2004, 05:36 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old September 16th, 2004, 10:36 AM
Friend of Wrox
 
Join Date: Aug 2004
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

 
Old September 16th, 2004, 11:37 PM
Friend of Wrox
 
Join Date: Aug 2004
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


 
Old September 18th, 2004, 03:47 PM
Friend of Wrox
 
Join Date: Aug 2004
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






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.