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


?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.