Click to See Complete Forum and Search --> : Ok, trying to work with sessions


polorboy
01-10-2007, 03:39 PM
Alright, I am working through yet another example (if anyone has noticed, i do that a lot, lol) from a book and trying to get it to fit what I want to do. The thing is that I haven't changed the code from what the book has (except where it needs to match my specific settings and file names) and it doesn't want to work. I went to the book publishers website and downloaded the files and they work fine when I run them.

I have a auth.inc.php file with this php code in it, all in <body>:


<?php
session_start();
if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
header("Refresh: 5; URL=upload.php?redirect=$redirect");
print "You are being sent to the file upload page.<br/>";
print "If your browser does not redirect you in 5 seconds <a href=\"upload.php?redirect=$redirect\">click here</a> to continue.";
}
else {
$redirect = $_SERVER['PHP_SELF'];
header("Refresh: 5; URL=login.php?redirect=$redirect");
print "You will be redirected to the login page.<br/>";
print "If your browser does not redirect you in 5 seconds <a href=\"login.php?redirect=$redirect\">click here</a> to continue.";
die();
}
?>


And a login.php page with this code, also all in <body>:


<?php
session_start();
$_SESSION['logged'] = 0;

if (isset($_POST['username'] == "username" && $_POST['password'] == "password") {
$_SESSION['logged'] = 1;
header("Refresh: 5; URL=" . $_POST['redirect'] . "");
print "You are being sent to the file upload page.<br/>";
print "If your browser does not send you in 5 seconds please <a href-\"" . $_POST['redirect']. "\">Click Here</a> to continue.";
}
else {
?>


<form enctype="text/plain" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" class="login">
<fieldset>
<legend>
Login
</legend>
<label for="username">
Username:<br />
<input type="text" name="username" id="username" size="20" maxlength="20" />
<br />
<br />
</label>
<label for="password">
Password:<br />
<input type="password" name="password" id="password" size="20" maxlength="20" />
<br />
<br />
</label>
<input type="submit" name="login" id="loginbtn" value="Login" />
</fieldset>
</form>


And a upload.php file with this code in it:

before the <html>:

<?php
include "auth.inc.php";
?>


down in the <body>:

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data" method="post">
<fieldset>
<legend>
Upload File
</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />
<label for="fileupload">
<br />Your file:<br />
<input type="file" name="fileupload" />
</label><br /><br />
<input type="submit" value="Upload" />
</fieldset>
</form>


<?php
$file_dir = "..\\file\\upload\\directory";
foreach($-FILES as $file_name => $file_array) {
echo "path: ".$file_array['"tmp_name"']."<br/>\n";
echo "path: ".$file_array['"name"']."<br/>\n";
echo "path: ".$file_array['"type"']."<br/>\n";
echo "path: ".$file_array['"size"']."<br/>\n";

If(is_uploaded_file($file_array['"temp_name"'])){
move_uploaded_file($file_array['"tmp_name"'], "$file_dir/".$file_array["name"]) or die("Couldn't copy");
print "File was uploaded successfully!<br/>";
}
}
?>


Now, what is supposed to happen is when the user clicks the link to upload a file it points to the upload.php page, and it checks to make sure that they are logged in by checking their session. If they are not logged in it is supposed to redirect them to the login.php page and if they are logged in it should let them see the upload.php page and upload a file. What does happen is when I try to open upload.php I just get a blank page. Also, if I just open up the auth.inc.php page it says I am already logged in and brings me to the upload page, which also shows up as a blank page. Do I have the php code in the wrong place on the page, or is there something I am just not seeing? Thanks.

NightShift58
01-10-2007, 03:45 PM
if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
header("Refresh: 5; URL=upload.php?redirect=$redirect");
...
The variable $redirect hasn't been set.

polorboy
01-10-2007, 03:51 PM
...:o ...:confused: ... ok, but if I type into my browser the direct link to the auth.inc.php and it tries to rediect me to the correct page, just the page doesn't load.

NightShift58
01-10-2007, 04:11 PM
... because upload.php probably doesn't know how to handle redirect=''

polorboy
01-10-2007, 04:54 PM
Well, I did find one problem in my upload.php file. I have $-FILE in my php script in the body, I changed it to $_FILE and now the page loads up to the point of printing out that it is going to redirect me but then doesn't get any further.

NightShift58
01-11-2007, 02:12 AM
I've tested the redirect in "auth.inc.php" on my server and it works fine.

Can you post upload.php?

polorboy
01-11-2007, 09:01 AM
Ok, well, I am going to try do redo this from scratch. There a ton of problems that I am finding in my code and the code from the book. So, I am going to try a different approach.