Page 1 of 1

passing sessions to another page

Posted: Wed Nov 29, 2006 8:44 pm
by ftd
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

I’m trying to learn how to use sessions instead of POST/GET protocols. I’m quite inexperienced but most of the progress print checks have done what I think I want them to (Now commented out on script). My troubles start at after the registering of the sessions. The print check works here, but what I want to do is to pass the sessions onto a new page and remember the sessions. 

Therefore, if I am on the right track with this code, I need the link syntax to a page e.g. loggedIn.php and then have the $username printed i.e. Hello username…

Hope this is clear enough.

Regards,

FTD

Code: Select all

<?PHP

  	session_start();
   // print "session started ";

	//include library files for mysql connection 
	include 'libs/config.php'; // variables
	include 'libs/opendb.php'; // open connection


	// username and password posted from sign in form
	$username = $_POST['username'];
	$password = $_POST['password'];

	//prepare sql query
	$sql="SELECT * FROM members WHERE username='$username' and password='$password'";

	//test sql query (OK)
	//print("<p>SQL $sql</p>");

	//set variable for mysql query
	$result=mysql_query($sql);

	// Mysql_num_row counts table row
	$count=mysql_num_rows($result);
    //test print OK
	//print "$count  check";

	// if rows exist place into rows (array)
	if ($count == 1){
	
	   $_SESSION['id'] = $row[0];
	   $_SESSION['class'] = $row[1];
	   $_SESSION['username'] = $row[2];
	   $_SESSION['password'] = $row[3];
	   $_SESSION['firstName'] = $row[4];
	   $_SESSION['lastName'] = $row[5];
	   $_SESSION['email'] = $row[6];
	   $_SESSION['country'] = $row[7];
	   $_SESSION['description'] = $row[8];	
	   //test print OK
	   //print "success";
	   
	//check username and password are sent (OK)
	print("<p>Username $username</p>");
	print("<p>Password $password</p>");	
		   
//trouble starts here	
//need to pass to a new page and pass these session variables over
	
	} else
    {

		echo "Sorry, wrong Username or password, try again";
		print("<p><a href = 'main_login.php'>Link back home</a></p>");
	}; //end else


?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Nov 29, 2006 9:13 pm
by LiveFree
You can use the header() function to reidrect the page, and keep all the session variables (provided you put session_start() in the second page as well)

You cannot have any output to the browser before the call to session_start() or header(), however

Posted: Wed Nov 29, 2006 9:15 pm
by Zoxive
As long as you have session_start(); on top of the page, it will work.

Posted: Wed Nov 29, 2006 11:24 pm
by ftd
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


OK, thanks for the quick reply. For the main, the code is verbatim except for the header function that I included (See example below e.g. header(‘location:login.php’);

Code: Select all

// if rows exist place into rows (array)
	if ($count == 1){
	
	   $_SESSION['id'] = $row[0];
	   $_SESSION['class'] = $row[1];
	   $_SESSION['username'] = $row[2];
	   $_SESSION['password'] = $row[3];
	   $_SESSION['firstName'] = $row[4];
	   $_SESSION['lastName'] = $row[5];
	   $_SESSION['email'] = $row[6];
	   $_SESSION['country'] = $row[7];
	   $_SESSION['description'] = $row[8];	
	   //test print OK
	   //print "success";
	    header('location:login.php');
	//check username and password are sent (OK)
	
	} else
This header function passes onto login.php. here is the code of login:

Code: Select all

<?php
session_start();
	
print("<p>Class $class</p>");
print("<P>UN: $username</p>");
print("<p>Password: $password</p>");
print("<p>First Name: $firstName</p>");
print("<p>Last name: $lastName</p>");
print("<p>Email: $email</p>");
print("<p>Country: $country</p>");
print("<p>Description: $description</p>");
print("<p><a href = 'main_login.php'>Link back home</a></p>");
?>

This outputs the print statements but not the variable?

Cheers,

PeterK


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Nov 29, 2006 11:50 pm
by Zoxive

Code: Select all

<?php
session_start();
	
print("<p>Class " . $_SESSION['class'] . "</p>");
print("<P>UN: " . $_SESSION['username'] . "</p>");
print("<p>Password: " . $_SESSION['password'] . "</p>");
print("<p>First Name: " . $_SESSION['firstName'] . "</p>");
print("<p>Last name: " . $_SESSION['astName'] . "</p>");
print("<p>Email: " . $_SESSION['email'] . "</p>");
print("<p>Country: " . $_SESSION['country'] . "</p>");
print("<p>Description: " . $_SESSION['description'] . "</p>");
print("<p><a href = 'main_login.php'>Link back home</a></p>");
?>
You need to tell it to get 'class' or w/e from the session varible.

And put php tags around the code so it highlights it making it easier to read. : p

Re: passing sessions to another page

Posted: Thu Nov 30, 2006 1:01 am
by timvw
ftd wrote:

Code: Select all

// username and password posted from sign in form
	$username = $_POST['username'];
	$password = $_POST['password'];
What happens when they are not submitted?

Code: Select all

// make sure that username and password are submitted
if (isset($_POST['username']) && isset($_POST['password'])) {
}
ftd wrote:

Code: Select all

//prepare sql query
	$sql="SELECT * FROM members WHERE username='$username' and password='$password'";
That's not preparation for a mysql query... What happens if you have a username O'Reilly?

Code: Select all

$mysql = array();
$mysql['username'] = mysql_real_escape_string($_POST['username']);
$mysql['password'] = mysql_real_escape_string($_POST['password']);

$sql = "SELECT * FROM members WHERE username='" . $mysql['username'] . "' AND password='". $mysql['password'] . "'";
ftd wrote:

Code: Select all

//set variable for mysql query
	$result=mysql_query($sql);

	   $_SESSION['id'] = $row[0];
	   $_SESSION['class'] = $row[1];
	   $_SESSION['username'] = $row[2];
	   $_SESSION['password'] = $row[3];
	   $_SESSION['firstName'] = $row[4];
	   $_SESSION['lastName'] = $row[5];
	   $_SESSION['email'] = $row[6];
	   $_SESSION['country'] = $row[7];
	   $_SESSION['description'] = $row[8];
Where does $row come from? It seems that you've forgotten to fetch a row from the resultset...

Code: Select all

row = mysql_fetch_row($result);

$_SESSION['id'] = $row[0];

Posted: Thu Nov 30, 2006 10:15 pm
by ftd
Hi All,

Is there a problem with the if statement of the $count rows part? I have been checking to see if this statement is working and is only printing out the statement and not the variable. I'm not ignoring advice, I just want to get one thing working at a time, once I understand, then I'll attempt a bit more.

Code: Select all

<?PHP
	session_start();
	//print("session started");
	
	//include library files for mysql connection 
	include 'libs/config.php'; // variables
	include 'libs/opendb.php'; // open connection


	// username and password posted from sign in form
	$username = $_POST['username'];
	$password = $_POST['password'];

	//check username and password are sent (OK)
	//print("<p>Username $username</p>");
	//print("<p>Password $password</p>");	
	
	//prepare sql query
	$sql="SELECT * FROM members WHERE username='$username' and password='$password'";

	//test sql query (OK)
	//print("<p>SQL $sql</p>");

	//set variable for mysql query
	$result=mysql_query($sql);

	// Mysql_num_row counts table row
	$count=mysql_num_rows($result);

	// if rows exist place into rows (array)
	if ($count == 1){
		$_SESSION['id'] = $row[0];
		$_SESSION['class'] = $row[1];
		$_SESSION['username'] = $row[2];
		$_SESSION['password'] = $row[3];
		$_SESSION['firstName'] = $row[4];
		$_SESSION['lastName'] = $row[5];
		$_SESSION['email'] = $row[6];
		$_SESSION['country'] = $row[7];
		$_SESSION['description'] = $row[8];

		//test print OK
		print "success";
		print "$count";
		//header("location:login.php");
		//check username and password are sent (OK)

		print("<p>Class " . $_SESSION['class'] . "</p>");
		print("CLASS:::: $class");

	
	} else {
		echo "Sorry, wrong Username or password, try again";
		print("<p><a href = 'main_login.php'>Link back home</a></p>");
	}; //end else

?>

Posted: Thu Nov 30, 2006 10:33 pm
by John Cartwright
Zoxive wrote:As long as you have session_start(); on top of the page, it will work.
I just thought I would point this out again, since you seemed to have skipped over it Image

Posted: Thu Nov 30, 2006 11:07 pm
by ftd
Fair enough, thanks, i had forgotten it. Instead of putting the correction in, i just edited my last post with the session_start(); function. however, the print statements still only printout the statement and not the session variables i.e.

Code: Select all

print("<p>Class " . $_SESSION['class'] . "</p>");


Output: Class