how to use session()

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

nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

how to use session()

Post by nishmgopal »

Hi guys

I am trying to pass variables across various pages and I believe sessions is the best way, but Im afraid I keep getting the following error on the page where my session starts:

Parse error: syntax error, unexpected T_VARIABLE

the code for this page is:

Code: Select all

 
<? session_start() 
$getjob = $_SESSION['job'];
 
?>
I am assiging the variable $getjob with the form and all the ID's and the names are matching - job.

And on all the other pages I am using the following code:

Code: Select all

 
<? session_start() 
    
    $_SESSION['job'] = $_POST['job'];
    
    ?>
 
Can anyone help me? I would like to echo $_POST ['job'] on various pages.

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to use session()

Post by califdon »

First, never use the "short" PHP opening tag. In other words, always use <?php.

Second, you're missing the semicolon at the end of session_start(); That causes the next line to be read as part of the first line, which causes the parser to see the variable $getjob or $_SESSION where it's not expecting a variable, which is what the error message said.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

thanks for that. But I still cant seem to echo it on other pages this is my code on one of the other pages I want to use 'job' in:

top of the page:

Code: Select all

 
 
<?php session_start();
 
$_SESSION['job'] = $_POST['job'];
 
 ?>
 
            <h1 class="title2">Find Suitable Candidate: <?php echo $_POST['job'] ?></h1>
            <?php
Then I have an sql statement on the same page:

Code: Select all

 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$_POST['job']";
 
What am i doing wrong?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: how to use session()

Post by jayshields »

I trust that on the other pages you are also calling session_start() at the top and referencing job with $_SESSION['job'] and not $_POST['job'].

In the example you posted you are getting into some trouble with the quotes. You missed a single quote out of the SQL query and you also tried to use single quotes inside single quotes. In other words, change this:

Code: Select all

$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$_POST['job']";
to this:

Code: Select all

$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . $_POST['job'] . "'";
I would strongly recommend that you use mysql_real_escape_string() with every variable which you are passing from the user into a database, which would result in this:

Code: Select all

$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_POST['job']) . "'";
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

the sql is still returning a blank screen, with no errors. The top of my page looks like this:

Code: Select all

<? session_start(); 
    
    $getjob = $_SESSION['job'];
    
    ?>
 
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to use session()

Post by califdon »

Although it's probably not the problem, you are not following instructions very well. You are still using the short tag.
this is my code on one of the other pages I want to use 'job' in:

Code: Select all

$_SESSION['job'] = $_POST['job'];
Well, that's assigning a value to the session variable, not reading it.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

Sorry I have changed my code to the long tag.

But How do I read the value job?
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: how to use session()

Post by JeffG »

How about

Code: Select all

 
$job = $_SESSION['job'];
 
??
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

I tried that already,

I had:

Code: Select all

<? php session_start(); 
    
    $getjob = $_SESSION['job'];
    
    ?>
But again all I get is a blank screen :(
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: how to use session()

Post by JeffG »

There is really not enough information to begin to understand your problem. Perhaps if you showed the <form> statement with the action and method and the part of the form body where the value of 'job' is set it would help.

Also your first post looks as though it's the wrong way round. Do you really have multiple pages with forms setting the job variable, and a single page getting the value from the session variable?
Last edited by JeffG on Mon Mar 09, 2009 7:02 am, edited 1 time in total.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

seeing the above topics i thought i would add:

If you are trying to get the variable 'job' from the post of a form.

Code: Select all

 
$job=$_POST['job']; //lets say this is page1.php
 
If you want the variable job to be used in the same page

Code: Select all

 
 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$job'";
 
If you want to use the variable in another page(say page2.php)

Code: Select all

 
<?php
session_start();
$_SESSION['job'] = $_POST['job']; //write this in page1.php
?>
 

Code: Select all

 
<?php
session_start();
//write this in page2.php
 
$job = $_SESSION['job'];
//all further mysql queries
?>
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

I have done all the above but all I keep getting is a blank screen. The full code is broken down below:

Page 1, where the form is held:

Code: Select all

 
<?php session_start();
 
$_SESSION ['job'] = $_POST['job'];
 
?>
 
<?php
$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
 
$dbname=@mysql_select_db($dbname, $conn)
           or die("Couldn't select database");
           ////
           
     $query = "SELECT * FROM Job_Spec";
    $result = mysql_query($query);
    
    echo "<h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
     
    <form id='form2' method='post' action='go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       $Project_Name= $row['Project_Name'];
       echo "<option value=\"$Project_Name\">$Project_Name</option>";
   }
   
 echo "</select></label></p>";
  ?>
 
Page 2 - Where I am trying to use the variable in a sql statement:

Code: Select all

 
<? 
session_start(); 
    
$getjob = $_SESSION['job'];
    
    ?>
 
<?php
$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
 
$dbname=@mysql_select_db($dbname, $conn)
           or die("Couldn't select database");
 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
 
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
.....
 
 
In the sql statement, if i replace $getjob with an actual name ie Manager the query works fine.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

in page 1

Code: Select all

 
if(isset($_POST['job']))
{
$_SESSION ['job'] = $_POST['job'];
}
 
in page 2

Code: Select all

 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
echo "$query1";//just to check the actual query being passed.
 
           
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query mysql_error()"); //check whats the error before exiting
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

susrisha wrote:in page 1

Code: Select all

 
if(isset($_POST['job']))
{
$_SESSION ['job'] = $_POST['job'];
}
 
in page 2

Code: Select all

 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
echo "$query1";//just to check the actual query being passed.
 
           
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query mysql_error()"); //check whats the error before exiting
 
I tried that and it echos :

$query1 = "SELECT * FROM Job_Spec WHERE Project_Name =''

Then I try and runt the query and its back to the blank screen!
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: how to use session()

Post by JeffG »

Couple of assumptions I'm making:
  1. Somewhere lower down Page 1 you have a submit button and then close off the form.
  2. Page 1 itself is 'go.php', otherwise the assignment to the session variable will only take place when Page 1 is first executed, and not when the form is submitted and the post actually has something in it.
Post Reply