Jump to content

session, post, get not working


anotherphpnoob

Recommended Posts

As the title says session, post and get are not working. This is what i use to initialize them. For get just switch session with get.

 

foreach ($row as $val)
          {  
            $_SESSION[$rowcount] = $val;
//              echo "<td>$val</td>\n";
                $rowcount=$rowcount+1;
      }

 

when i was using session i would check to make sure that they were working with a couple of these lines in the same php document.

 

echo "Session row 1 = ".$_SESSION[0]."<br />";
echo "Session row 2 = ".$_SESSION[1]."<br />";

 

then on the next page where I was trying to receive the variables i was using this just to start off with and make sure i was getting the variables.

 

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

 

if i switch to post and get i get the same results. if there is an easier way to pass variables between pages id love to hear them

Link to comment
https://forums.phpfreaks.com/topic/209729-session-post-get-not-working/
Share on other sites

script.php

 

<?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'];
   $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;
	//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

 

<?php

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


?>

the SQL query is fine. i built the page to show me that it was working. I then commented out all the stuff because i didnt want it to show up and built the rest of the stuff so that when it does work it, $_SESSION gets its variables and then goes to index.php.

this code is one </table> short but if you look in the code i posted before its there.

 

 // echo "<table>\n";
  //echo "<tr>\n"; 
      foreach ($row as $val)
  {  
    $_SESSION[$rowcount] = $val;
	//echo "<td>$val</td>\n";
	$rowcount=$rowcount+1; 
	$count=$count+1;
      } 
      //echo "</tr>\n";
  
  }

 

I also tested whether or not $_SESSION was even getting anything from that statement later on in the page, and then proceeded to comment that out as well.

 

  //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];

error_reporting was set to E_ALL however display_errors was not set to ON.

both of these are now set to the following.

display_errors = ON

error_reporting = E_ALL

 

ran through the scripts as usual, not sure what im looking for, or what should or should not be happening.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.