Sessions question

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

Post Reply
Brandensmith1
Forum Newbie
Posts: 9
Joined: Tue Sep 11, 2007 3:43 pm

Sessions question

Post 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 :(
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

remeber to exit

Post 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".
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Also note that full URLs, http:// and all, are required to maintain standards compliance in header() based redirections.
Brandensmith1
Forum Newbie
Posts: 9
Joined: Tue Sep 11, 2007 3:43 pm

Post 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?
Brandensmith1
Forum Newbie
Posts: 9
Joined: Tue Sep 11, 2007 3:43 pm

Post 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
Brandensmith1
Forum Newbie
Posts: 9
Joined: Tue Sep 11, 2007 3:43 pm

Post 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'];
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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.
Post Reply