Sessions Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Sessions Issue

Post by Saethyr »

Have never used them, never been a need until now so I am curious why the following is not working

Code: Select all

<?php

session_start() //Top of every page


function getID($fname, $lname)
	{
		//Check for Session ID
		if (!$PHPSESSID)
			{
				session_register("TechID")
			}
			else if (!$TechID)
			{
				session_register("TechID")
			}
		
		//Db Connection
		$connection = mysql_connect ("localhost", $DbUsername, $DbPassword); 
		mysql_select_db ($DbName, $connection); 
		
		//SQL for inserting new Data
		$sql = "SELECT TechID from techinfo where FName = '$fName' AND LName = '$lName'";
		
		//Execute Query
		mysql_query($sql);
		
		//Assign Value to Session
		while ($row = mysql_fetch_array($sql_result)) {
			$_SESSION["TechID"] = $row["TechID"];
					
		}
	}
?>
I am calling this function from another page, whioch my functions page is included as well, but, I can pull the session ID by not $TechID. Any help would be appriciated.


Saethyr
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

session_start needs a semi colon after it.

session_start();
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

yeah it's there, I just left it out on here because I typed it in, the rest is pasted.
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Not sure if it is the answer but I always use single quotes here.

$_SESSION['TechID'] = $row['TechID'];
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Think this should fix it mate

Code: Select all

<?php 

session_start(); //Top of every page 


function getID($fname, $lname) 
   { 
      //Check for Session ID 
      if (!$PHPSESSID) 
         { 
            session_register("TechID") 
         } 
         else if (!$TechID) 
         { 
            session_register("TechID") 
         } 
       
      //Db Connection 
      $connection = mysql_connect ("localhost", $DbUsername, $DbPassword); 
      mysql_select_db ($DbName, $connection); 
       
      //SQL for inserting new Data 
      $sql = "SELECT TechID from techinfo where FName = '$fName' AND LName = '$lName'"; 
       
      //Execute Query 
      $sql_result = mysql_query($sql); 
       
      //Assign Value to Session 
      while ($row = mysql_fetch_array($sql_result)) { 
         $_SESSION["TechID"] = $row["TechID"]; 
                
      } 
   } 
?>
you haven't set $sql_result.

Mark
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

I should wake up before I start posting. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

...and BTW, it doesn't matter if you use single or double quotes

Mark
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Hell Paddy I should wake up before I start coding I guess.


Thanks Bech I knew it would take another set of eyes ;)


Saethyr
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Now to get the Session to carry yto another page is it just

Code: Select all

<?
session_start();

echo "$TechID"; //Is this the Way?


php

?>
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

$TechID = $_SESSION['TechID'];
echo $TechID;
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Thanks Paddy

:D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just a heads up, don't use session_register() and $_SESSION together - they don't play nice and you generally get fairly buggy results. In fact you shouldn't use session_register() at all (it's deprecated and doesn't work with register_globals off). In your function you can just delete:

Code: Select all

//Check for Session ID
      if (!$PHPSESSID)
         {
            session_register("TechID")
         }
         else if (!$TechID)
         {
            session_register("TechID")
         }
the following line:

Code: Select all

$_SESSION["TechID"] = $row["TechID"];
makes that code uneccessary.

Mac
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Thanks Twig!

Still was not working last night so I will try this when I get home to see if I can make it work. coming from ASP sessions these are bit more difficult to make work it seems.


Saethyr
Post Reply