Page 1 of 1

Sessions question

Posted: Thu Sep 13, 2007 12:15 am
by Brandensmith1
I know I'm asking a lot of problems but this is the first time I've done something with mysql so I'm kinda learning as I go.

Code: Select all

$b = 0;
if (!isset($_SESSION['login']))
{
	header("Location:login.php");
}
if(isset($_SESSION['login']))
{
$query = "SELECT * FROM newsart GROUP BY count ORDER BY count desc";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$b++;
	if($_POST['checked_'. $row['count'] .''] == $row['count'])
	{
		$_SESSION[''.$row['count'].''] = $row['count'];
	}	
}
for($j = 1; $j <= $b; $j++)
{
if(isset($_SESSION['$j']))
{
	session_write_close();
	header('location:delete.php');
}
Making the session isnt the problem, its calling it later. My

Code: Select all

if(isset($_SESSION['$j']))
{
	session_write_close();
	header('location:delete.php');
}
Statement seems to me that if $_SESSION had a value of $j (in this case any number between the first and last row of my mysql table) it should echo the row count. At first I had assumed it wasn't creating the sessions but then I experimented and just tried echo $_SESSION['1']; and it echo'ed the correct value of 1. so why isnt the $_SESSION['$j'] working? It doesn't seem to want to save the session information over to delete.php either, even when i tried sticking in

Code: Select all

if(isset($_SESSION['1']))
{
session_write_close();
header('location:delete.php');
}
after the while loop. The delete.php page just comes up blank.

delete.php

Code: Select all

<?php
session_start();
include '*****';
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_SESSION['login']))
{
	header('Location: index.php');
}
else if(isset($_SESSION['login']))
{
echo $_SESSION['1']; //testing purposes only
$query = "SELECT * FROM newsart GROUP BY count ORDER BY count desc";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
	{
		echo $_SESSION[''. $row['count'] .''];
	}
}
?>
Could someone enlighten with your grand knowledge of mysql so I can fix this mess :(

remeber to exit

Posted: Thu Sep 13, 2007 12:24 am
by yacahuma
Remember to exit after header('Location')

from the php.net
When not using "exit;" after the header("Location: ..."), the execution of the script continue below the header, until the end is reached. After this, it's doing the "Redirect".

Posted: Thu Sep 13, 2007 7:23 am
by feyd
Also note that full URLs, http:// and all, are required to maintain standards compliance in header() based redirections.

Posted: Thu Sep 13, 2007 3:47 pm
by Brandensmith1
Ah. Okay i added the exit however it seems to not even register what the for loop is saying.

Code: Select all

for($j = 1; $j <= $b; $j++)
{
if(isset($_SESSION['$j']))
{
	echo $_SESSION['$j'];
	session_write_close();
	header('location:http://partytime.x10hosting.com/news/delete.php');
	exit();
}
when a value is checked it sets $_SESSION['the number of the row'] and I have $b as the total number of rows, so you'd think when it gets to $_SESSION['1 or whatever number is checked'] it would execute the header, but its not 8O any ideas?

Posted: Thu Sep 13, 2007 3:58 pm
by Brandensmith1
nevermind, figured it out :oops: I just had to change $_SESSION['$j'] to $_SESSION[''.$j.''] silly mistake, however; the sessions made on this page are still not sending over to delete.php

Posted: Thu Sep 13, 2007 5:03 pm
by Brandensmith1
ha, figured that one out myself too. php.net said you cant have a session whose name is only a number so i just changed $_SESSION[''.$row['count'].''] = $row['count']; to $_SESSION['num'.$row['count'].''] = $row['count'];

Posted: Thu Sep 13, 2007 7:38 pm
by Stryks
No real problem with what you have from what I can see, however ...
$_SESSION['$j'] to $_SESSION[''.$j.'']
... could also just be expressed as ...

Code: Select all

$_SESSION[$j];
Likewise ...
$_SESSION['num'.$row['count'].''] = $row['count'];
... would look cleaner as ...

Code: Select all

$_SESSION['num'.$row['count']] = $row['count'];
If you have a think about it, the hows and whys are probably clear.