/    Sign up×
Community /Pin to ProfileBookmark

Ok, trying to work with sessions

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

[code=php]
<?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();
}
?>
[/code]

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

[code=php]
<?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 {
?>
[/code]

[code=html]
<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>
[/code]

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

before the <html>:

[code=php]
<?php
include “auth.inc.php”;
?>
[/code]

down in the <body>:

[code=html]
<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>
[/code]

[code=php]
<?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/>”;
}
}
?>
[/code]

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.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 10.2007 — [code=php]if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
header("Refresh: 5; URL=upload.php?redirect=$redirect");
...[/code]

The variable $redirect hasn't been set.
Copy linkTweet thisAlerts:
@polorboyauthorJan 10.2007 — ...:o ...? ... 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.
Copy linkTweet thisAlerts:
@NightShift58Jan 10.2007 — ... because upload.php probably doesn't know how to handle [b]redirect=''[/b]
Copy linkTweet thisAlerts:
@polorboyauthorJan 10.2007 — 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.
Copy linkTweet thisAlerts:
@NightShift58Jan 11.2007 — I've tested the redirect in "auth.inc.php" on my server and it works fine.

Can you post upload.php?
Copy linkTweet thisAlerts:
@polorboyauthorJan 11.2007 — 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.
×

Success!

Help @polorboy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...