Page 1 of 1

session, post, get not working

Posted: Wed Aug 04, 2010 1:21 am
by anotherphpnoob
i know the titles says session, get and post not working and that's partly true. post works in lines 19 and 18 when the data is coming from a html form on the previous page, however it does not work when going to to index.php. You will notice that index.php doesnt really do anything, this is because it is a test page so i know when the $_SESSION variables are working. I know that my name implies that i am a noob at php and while this is true i am not a coding noob. there is nothing wrong with my sql query, there is data going into the $_SESSION variables on line 36 and my if statements do what they are supposed to. I have tested all of these and as far as i can tell the only thing i am doing wrong is with the $_SESSION variable and for some reason it isnt being carried over to index.php. any suggestions would be appreciated.

display_errors = ON
error_reporting = E_ALL

script.php

Code: Select all

<?php 
session_start();
//database server 
define('db_server', 'localhost'); 

//user, password, and database variables 
$db_user = 'user'; 
$db_password = 'password';     
$db_dbname = 'people'; 


/** 
* Run MySQL query and output  
* results in a HTML Table 
*/ 
function outputQueryResults() { 

   $username=$_POST['name'];  //line 18
   $password=$_POST['pass'];
   $count=0;
   $rowcount=0;

  //run a select query 
  $select_query = "SELECT * FROM names WHERE username= '".$username."' AND password = '".$password."'"; 
  $result = mysql_query($select_query); 
  //output data in a table 

  while ($row = mysql_fetch_row($result))
  {

	  
	 // echo "<table>\n";
	  //echo "<tr>\n"; 
      foreach ($row as $val)
	  {  
	    $_SESSION[$rowcount] = $val; //line36
		//echo "<td>$val</td>\n";
		$rowcount=$rowcount+1; 
		$count=$count+1;
      } 
      //echo "</tr>\n";
	  
  } 
  if(is_null($row)||empty($row)&& $count==0)
  {
		if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) 
		{
			$uri = 'https://';
		} 
		else 
		{
			$uri = 'http://';
		}
		$uri .= $_SERVER['HTTP_HOST'];
		header('Location: '.$uri.'/error.html');
  }else{

  //echo '</table>';
//echo "Session row 1 = ".$_SESSION[0]."<br />";
//echo "Session row 2 = ".$_SESSION[1]."<br />";
//echo "Session row 3 = ".$_SESSION[2]."<br />";
//echo "Session row 4 = ".$_SESSION[3];
	if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) 
	{
		$uri = 'https://';
	} 
	else 
	{
		$uri = 'http://';
	}
	$uri .= $_SERVER['HTTP_HOST'];
	header('Location: '.$uri.'/index.php');

  }
} 

//connect to the database server 
$db = mysql_connect(db_server, $db_user, $db_password); 
if (!$db) { 
   die('Could Not Connect: ' . mysql_error()); 
} else { 
  echo "Connected Successfully...\n"; 
} 
//select database name 
mysql_select_db($db_dbname); 

//run query and output results 
outputQueryResults(); 

//close database connection 
mysql_close($db) 
?> 
index.php

Code: Select all

<?php

session_start();
if(!isset($_SESSION['1'])){
echo "help1";}else{$var=$_SESSION['1'];
}
echo $_SESSION['1'];
echo $var;

?>

Re: session, post, get not working

Posted: Wed Aug 04, 2010 2:09 am
by internet-solution
I notice two things.

In script.php, your $uri variable is $uri = 'http://' snd then you are adding '/index.php' to it. So the resulting URL will be 'http:///index.php' - three slashes which is not right.

In index.php, there is a blank line before session_start() - this creates issues with session.