LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-05-2005, 01:52 AM   #1
prabhatsoni
Member
 
Registered: Oct 2004
Location: India
Distribution: FC 12
Posts: 233

Rep: Reputation: 30
php session variables not preserved when navigating thru links


Hello everybody,
I have a collection of php scripts wherein I am preserving the user name and password (collected in the first script) as $_session['user_name'] etc session variables. In the subsequent scripts where I collect other inputs from the users through HTML-Forms, I am using the username and password preserved as above mentioned session variables to connect to a mysql database. As long as I use html forms and method="post" to navigate between different screen, I find I am able to preserve the session variables.
In one of the scripts I want the user to go back to the previous form, if the user has not filled up all the required form fields. For this purpose I created a link which links to the previous script (containing the form). But now I find that the session vaiables vanished. Why should it be ? I am of the opinion that the session vaiables are preserved even when navigating through hyper links. The scripts are:

The form (uer_auth.php)
Code:
.
	.
	.
   $_SESSION['user']=$_POST['user_name'];
     $_SESSION['passwd']=$_POST['password'];
     $link=mysql_connect("localhost",$_SESSION['user'],$_SESSION['passwd'])
         or die("Can not connect. Check user name and password.");
     mysql_select_db("pali") or die("Cannot open database");
     echo "<p align=\"center\"><font size=\"7\">Menu for administrator</font></p><br /><br />";
     echo "<form method=\"post\" action=\"action.php\">";
     echo "Add complaint:<input type=\"radio\" name=\"choice\" value=\"1\"><br /><br />";
     echo "Close the complaint:<input type=\"radio\" name=\"choice\" value=\"2\">";
     echo "---->SDCA in which to close:";
     sdca_sel("sdca");
     echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
     echo "</form>";
	.	
	.
	.
action.php
Code:
.	
	.
   <?php
 switch($_POST['choice'])
   {
     case "1":
        add_case();
	break;
     case"2":
        close_case($_POST['sdca']);
	break;
     default:
	echo "No option !!";
   }
 ?>
The close_case() function
Code:
function close_case($arg)
  { if($arg=="Select SDCA")
      { echo "<p align=\"center\"><font size=\"6\">SDCA name not selected</font></p><br /><br />";
        echo "<a href=\"user_auth.php\">Go back to select SDCA</a>";
      }
  }
In the function close_case() I have provided the hyper link which calls back the first script. But there the user name etc not available. Can anybody help me in solving this simple riddle to me ?


Thanks in advance.


Prabhat Soni

Last edited by prabhatsoni; 06-05-2005 at 05:22 AM.
 
Old 06-05-2005, 08:51 AM   #2
huibert.alblas
Member
 
Registered: Jan 2004
Location: Duesseldorf /Germany
Distribution: Gentoo amd64 / Debian
Posts: 226

Rep: Reputation: 30
It only looks like this, but PHP behaves exactly like you tell it too :-)

in "user_auth.php"
in the first 3 lines you explity set the user name and password from post_vars.

If the link is followed later the post_vars are empty, you overwrite _SESSION['user'] with an empty string.

You should implement some beter session handling:

in user_auth.php (this is pseudo code and will likely not work exactly in this this way) :
Code:
if ($_SESSION['activated'] == false) {
  $_SESSION['user']=$_POST['user_name'];
  $_SESSION['passwd']=$_POST['password'];
$link=mysql_connect("localhost",$_SESSION['user'],$_SESSION['passwd'])
         or die("Can not connect. Check user name and password.");
  $_SESSION['activated']=true;
}
Here the session user will be set once if succselful.
Once it has been set, it cannot be unset.

So you will need something like "logout.php"
Code:
$_SESSION['activated'] = false;
echo "Logout, sucsefull, to use mysql, please login again";
Happy hacking.
 
Old 06-13-2005, 01:15 AM   #3
prabhatsoni
Member
 
Registered: Oct 2004
Location: India
Distribution: FC 12
Posts: 233

Original Poster
Rep: Reputation: 30
Thanks a lot Huibert,

You are right. I corrected it and now it is working. I should accept it was a very silly mistake.


Thanks again.



Prabhat Soni
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
php: session variables lost while redirecting appas Programming 2 10-13-2004 09:18 AM
How to use session variables in perl ? taqs Programming 1 09-28-2004 06:12 PM
PHP session variables in a database rmanocha Programming 0 07-16-2004 04:18 AM
PERL:: passing variables with links? ocularbob Programming 2 08-30-2003 09:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration