LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2004, 11:17 AM   #1
AskMe
Member
 
Registered: Jul 2003
Posts: 123

Rep: Reputation: 15
PHP Session problem!!!


Here is three pages which I want to connect and pass session variable to test.php, but this page comes blank.

Here are the page codes.

1. studentlogin.php
Code:
<html> 

<head> 
<title>student login</title> 
</head> 

<body> 

<p align="center"><u><font size="6" color="#800000">Student Login</font></u></p> 
<p align="center">&nbsp;</p> 
<form method="POST" action="studentauthenticate.php"> 
  <p align="center">&nbsp;</p> 
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1" height="37"> 
    <tr> 
      <td width="35%" height="17" align="right"><b>Matric:</b></td> 
      <td width="65%" height="17"><input type="text" name="matric" size="30"></td> 
    </tr> 
    <tr> 
      <td width="35%" height="19" align="right"><b>Pin:</b></td> 
      <td width="65%" height="19"><input type="text" name="pin" size="30"></td> 
    </tr> 
  </table> 
  <p align="center"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> 
</form> 

</body> 

</html>

2. Studentauthenticate.php

Code:
<?php 
$matric = $HTTP_POST_VARS['matric']; 
$pin= $HTTP_POST_VARS['pin']; 
session_start(); 
$HTTP_SESSION_VARS['matric']=$matric; 
$HTTP_SESSION_VARS['pin']=$pin; 
$db_conn = mysql_connect('localhost', '', ''); 
mysql_select_db('iium', $db_conn); 
$query="select * from login where matric='$matric' and pin='$pin'"; 
$result = mysql_query($query, $db_conn); 
if (mysql_num_rows($result)>0 ) 
               { 
      session_start(); 
               $HTTP_SESSION_VARS['matric']=$matric; 
               $HTTP_SESSION_VARS['pin']=$pin; 
              header('location:test.php'); 
                exit; 
             }  else 
        
              {  
             echo 'you cannot log in'; 
             echo 'please try again'; 
             require('studentlogin.php'); 
            
            exit; 
} 

        
?>
3. test.php


Code:
<?php 
$matric=$HTTP_SESSION_VARS['matric']; 
echo $matric 
?>
I want to get student matric, pass as session variable at test.php page, but it prints nothing, simply blank page, but in URL address I can see that I am in test.php page.
 
Old 02-12-2004, 12:05 PM   #2
Pcghost
Senior Member
 
Registered: Feb 2003
Location: The Real Washington
Distribution: Debian, Android
Posts: 1,819

Rep: Reputation: 46
Two points to look at here.

1. In order to use Session variables at all, the very first thing on each page should be
<?php
session_start();

2. It appears as your form is using POST variables to send the data, which is fine, but you must at some point convert them like this

session_register('matric')
$_session['matric'] = $_POST['matric']

The problem with the conversion you are performing could be in the syntax of your post to session conversion..

Last edited by Pcghost; 02-12-2004 at 12:07 PM.
 
Old 05-31-2004, 08:22 PM   #3
linux_pioneer
Member
 
Registered: May 2003
Distribution: Solaris 10, Solaris 8.0, Fedora Core 3
Posts: 203

Rep: Reputation: 30
pcghost,

I used that method when I 1st began working on my web page. I started off using post forms to send logon info throughout different pages which worked. Using the session handling functions made it much easier. One thing I am not sure is how to get a session to stay alive if a user opens a new window from the same system. My website has a page that is members only while all other pages are open to anybody. When members navigate from the members only page to open pages I use session function calls to keep them logged in with a username variable. The member page checks the username against my database to see if they are still logged in. This process works fine as long as the member stays within the same window. If the member opens a new browser window they have to logon again. Is there anyway to perserve the session beyond a browser window? I'm still brainstorming on this. Perhaps someone else has already figured it out.
 
Old 06-01-2004, 11:16 AM   #4
LuggerHouse
Member
 
Registered: May 2004
Location: Montreal,QC,Canada
Distribution: Fedora Core 7
Posts: 210

Rep: Reputation: 30
Save tyhe information in a database...

ref:

http://forums.devshed.com/archive/t-26714
 
Old 06-01-2004, 09:41 PM   #5
linux_pioneer
Member
 
Registered: May 2003
Distribution: Solaris 10, Solaris 8.0, Fedora Core 3
Posts: 203

Rep: Reputation: 30
I have the info in a database already. I have a boolean variable that turns to false when members log off and true when they login. My problem is getting the username when a user opens up a new window. At that point I need to figure out how to identify the user.
 
Old 06-02-2004, 06:28 AM   #6
LuggerHouse
Member
 
Registered: May 2004
Location: Montreal,QC,Canada
Distribution: Fedora Core 7
Posts: 210

Rep: Reputation: 30
Personnaly, what I use to do in those situation is recording the session_id in a cookie and in the db. Next time the user comes to the page, I get the old sessioin_id from the cookie and compare it to the one stored in the db. If there's a match so I can retreive the user's information based on that old session_id.
 
Old 06-02-2004, 07:32 AM   #7
linux_pioneer
Member
 
Registered: May 2003
Distribution: Solaris 10, Solaris 8.0, Fedora Core 3
Posts: 203

Rep: Reputation: 30
could you tell me how to do that or point me in a good direction. I've viewed tutorials from php.net, dev shed, and phpfreaks. like i said it works fine as long as the member is using the same window they used to login. i use session_register('username') on my login page and session_start() on the other pages. I also set the cookie on the home page. should i set a cookie every page? how do i use the cookie to get this user info?
 
Old 06-02-2004, 09:49 AM   #8
LuggerHouse
Member
 
Registered: May 2004
Location: Montreal,QC,Canada
Distribution: Fedora Core 7
Posts: 210

Rep: Reputation: 30
Ok, here's what I mean... Note this will be pseudo-code not php:

<?php
session_start();
if (! isset($username)){
if(checksession(session_id()) ){
//Check if there was a session with this user

}else{
//new user without old session
// display login page
}
}

function checksession(session_id){
$oldSession=checkCookie("oldSession") ; // This function should return the old sessionID
$record=checkDB($oldSession); //this function should return the ID of the corresponding line in the db
// query should look like
// select *from table where oldSession=$oldSession;

// check if the query returned a record
// if so
$username=$record[username]
}

if this is not clear enough, I will program you a demo page and put the sources available somewhere..

Dont give up!!
 
Old 06-02-2004, 08:59 PM   #9
linux_pioneer
Member
 
Registered: May 2003
Distribution: Solaris 10, Solaris 8.0, Fedora Core 3
Posts: 203

Rep: Reputation: 30
I understand what you are saying but I just can't figure out how the checkCookie function works to get the old session_id. If I could just figure out how to get that old session id. Do i need to set a cookie at every page and store it somehow?

Last edited by linux_pioneer; 06-03-2004 at 12:29 AM.
 
Old 06-03-2004, 07:33 AM   #10
LuggerHouse
Member
 
Registered: May 2004
Location: Montreal,QC,Canada
Distribution: Fedora Core 7
Posts: 210

Rep: Reputation: 30
Function checkcookie should look like this... This is real php but un tested...

This example has been taken from
http://ca2.php.net/manual/en/function.setcookie.php

I didn't bother about the mysql functionnality to get the record from the corresponding $old_session value ...

<?php

function checkCookie (){ // Finaly, no need for the session id as parm

global $_COOKIE,$old_session;

if (isset($_COOKIE['MyCookie'])) {
foreach ($_COOKIE['MyCookie'] as $name => $value) {
//echo "$name : $value <br />\n";
${$name}=$value; // Creates the variable and fill it. You should have $old_session=value afterward
}
}

if(isset($old_session) && $old_session !=""){

// compare this value in the database and take the correspondant records to fill
// The user's informations

}

// Then replace the information in the cookie by the new information:

$value = session_id()
setcookie("MyCookie[old_session]", $value);
// Also store the new value of old_session in the database..
?>

Hope it helped a bit!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 richard22 Programming 2 10-26-2004 05:50 AM
RH9: PHP session problem (or Apache problem) fengcn Red Hat 0 12-01-2003 06:32 PM
php session problem with windoze antken Linux - General 6 08-16-2002 08:28 AM
php session problem niehls Programming 3 06-20-2002 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 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