Forum Moderators: coopster

Message Too Old, No Replies

Carrying Username across pages

         

KeithScott

5:38 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



Hi all. Been trying to get this to work for a few days, to no avail. First, I have a user login on index.php. Then, they get redirected to audio3.php. Redirect works fine, BUT, I want that page to display files in the audio/username folder. I can't get the username from login page to carry over to audio page in "insert" where called for. New to php so the code may not look pretty.

Here is a portion of the code from the audio3.php page:

<?php

session_start();

//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
$tbl_name="authorize"; // Table name

//require the config file
require ("/home/zackpowe/public_html/downloads/config.php");
require ("/home/zackpowe/public_html/downloads/functions.php");

// Connect to server and select databse.
mysql_connect("$server", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Some functions
/**
* Get the contents of a directory.
* returns the contents in an array or false
* on completion.
*/
function getDirFiles($dirPath)
{
// if the passed var isn't a dir
if(!is_dir($dirPath))
{
// if debugging
echo 'Error: var is not a dir.';
return false;
}
// if we can't open the dir
if (!$handle = opendir($dirPath))
{
// if debugging
echo 'Error: can not open dir.';
return false;
}
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
$filesArr[] = trim($file);
}
}
closedir($handle);
sort($filesArr);
reset($filesArr);
return($filesArr);
}

while($row = mysql_fetch_array($result));
$downloadPath = "/home/zackpowe/public_html/downloads/";
$audioPath = $downloadPath ."audio/";
$path = $audioPath . $row['username']. "/";
$file_list = getDirFiles($path);
{
$file_list = getDirFiles("/home/zackpowe/public_html/downloads/audio/" . $row['username'] . "/");
$count = count($file_list);

?>
[/code]

dmmh

6:29 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



why is there another query for this anyway?
dont get me wrong, I do it the same way, but you could also opt to store the username in a session variable upon (correct) login; saves you another query.
There are a few things one can come up with why NOT to do this, but mostly it will be just fine.

if ($logged_in_correctly)){
//other session variables
$_SESSION['username'] = $row['username'];
}

then:

$downloadPath = "/home/zackpowe/public_html/downloads/";
$audioPath = $downloadPath ."audio/";
$path = $audioPath . $_SESSION['username']. "/";

dmmh

7:35 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



btw, you are selecting all users in the table, so $row['username'] just wont work because there are multiple values

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

what you COULD do is, upon succesful login, store the user's id from the table on the login.php page to a session variable ($_SESSION['user_id'] or something like that), then query for that specific id when you need the associated username

$sql="SELECT * FROM $tbl_name"." WHERE user_id =".mysql_real_escape_string($_SESSION['user_id']."";
$result=mysql_query($sql);

this is the most easy way for you I think

KeithScott

7:56 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



Thanks for your help. Should I put the _SESSION on both login and audio page or just one? If so, which one?

inveni0

8:14 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



I just use a standard form on an SSL page for login and have my user's account page filter to that form variable. Simple and effective enough. All they have to do on my site, though, is change their user data, which is simply stored in the same row as their password and such.

jatar_k

10:04 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Should I put the _SESSION on both login and audio page or just one? If so, which one?

you would set the $_SESSION variable to store username once they are authenticated via your login script.

You need to make sure you use session_start [php.net] on every page to be able to access the $_SESSION data

KeithScott

10:29 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



Hmm. I have taken the about suggestions and getting a parse error on line 20. I tried removing the ";" then get a Parse error: parse error, unexpected T_VARIABLE in /home/zackpowe/public_html/downloads/audio3.php on line 21

Im trying to figure out where to put the $_SESSION line...on the redirect.php page or on the audio3.php page?

Here is the latest code:

<?php

session_start();

//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
$tbl_name="authorize"; // Table name

//require the config file
require ("/home/zackpowe/public_html/downloads/config.php");
require ("/home/zackpowe/public_html/downloads/functions.php");

// Connect to server and select databse.
mysql_connect("$server", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name"." WHERE username=".mysql_real_escape_string($_SESSION['username'].""; //line 20
$result=mysql_query($sql);

// Some functions
/**
* Get the contents of a directory.
* returns the contents in an array or false
* on completion.
*/
function getDirFiles($dirPath)
{
// if the passed var isn't a dir
if(!is_dir($dirPath))
{
// if debugging
echo 'Error: var is not a dir.';
return false;
}
// if we can't open the dir
if (!$handle = opendir($dirPath))
{
// if debugging
echo 'Error: can not open dir.';
return false;
}
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
$filesArr[] = trim($file);
}
}
closedir($handle);
sort($filesArr);
reset($filesArr);
return($filesArr);
}

if ($logged_in_correctly)){
//other session variables
$_SESSION['username'] = $row['username'];
}

while($row = mysql_fetch_array($result));
$downloadPath = "/home/zackpowe/public_html/downloads/";
$audioPath = $downloadPath ."audio/";
$path = $audioPath . $row['username']. "/";
$file_list = getDirFiles($path);
{
$file_list = getDirFiles("/home/zackpowe/public_html/downloads/audio/" . $row['username'] . "/");
$count = count($file_list);

?>
<html>
<head>
<html>
<head>
<title>Zack Powers ¦ Client downloads.</title>
<link href="/style.css" rel="Stylesheet" rev="Stylesheet">
</head>

<body background="/images/background.gif" bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" topmargin=0 leftmargin=0 rightmargin=0>
<img src="/images/header.gif"><br />

<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>

<td width="110">
&nbsp;
</td>

<td width="*" class="regular">
<table border=0 width=60% bgcolor=0 cellpadding=5 cellspacing=0>
<tr>

<td bgcolor=#eeeeee style="font-family: Tahoma; font-size: 11px;"><b>
File Name (<? echo $_SESSION['username'];?>)
</b></td>

and the code continues...

KeithScott

10:52 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



Here is the latest based on the above suggestions...
I get the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zackpowe/public_html/downloads/audio3.php on line 62

Here is the latest code:
<?php

session_start();

//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
$tbl_name="authorize"; // Table name

//require the config file
require ("/home/zackpowe/public_html/downloads/config.php");
require ("/home/zackpowe/public_html/downloads/functions.php");

// Connect to server and select databse.
mysql_connect("$server", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Some functions
/**
* Get the contents of a directory.
* returns the contents in an array or false
* on completion.
*/
function getDirFiles($dirPath)
{
// if the passed var isn't a dir
if(!is_dir($dirPath))
{
// if debugging
echo 'Error: var is not a dir.';
return false;
}
// if we can't open the dir
if (!$handle = opendir($dirPath))
{
// if debugging
echo 'Error: can not open dir.';
return false;
}
while (false!== ($file = readdir($handle)))
{
if ($file!= "." && $file!= "..")
{
$filesArr[] = trim($file);
}
}
closedir($handle);
sort($filesArr);
reset($filesArr);
return($filesArr);
}

if ($logged_in_correctly){
//other session variables
$_SESSION['username'] = $row['username'];
}

while($row = mysql_fetch_array($result));
$downloadPath = "/home/zackpowe/public_html/downloads/";
$audioPath = $downloadPath ."audio/";
$path = $audioPath . $row['username']. "/";
$file_list = getDirFiles($path);
{
$file_list = getDirFiles("/home/zackpowe/public_html/downloads/audio/" . $_SESSION['username'] . "/");
$count = count($file_list);

?>
<html>
<head>
<title>Zack Powers ¦ Client downloads.</title>
<link href="/style.css" rel="Stylesheet" rev="Stylesheet">
</head>

<body background="/images/background.gif" bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" topmargin=0 leftmargin=0 rightmargin=0>
<img src="/images/header.gif"><br />

<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>

<td width="110">
&nbsp;
</td>

<td width="*" class="regular">
<table border=0 width=60% bgcolor=0 cellpadding=5 cellspacing=0>
<tr>

<td bgcolor=#eeeeee style="font-family: Tahoma; font-size: 11px;"><b>
File Name (<? echo $_SESSION['username'];?>)
</b></td>

and html continues...