www.webdeveloper.com
Results 1 to 7 of 7

Thread: Ok, trying to work with sessions

  1. #1
    Join Date
    Aug 2006
    Location
    New York/North Carolina
    Posts
    238

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

    PHP Code:
    <?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 Code:
    <?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 {
    ?>
    HTML Code:
    <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 Code:
    <?php
        
    include "auth.inc.php";
    ?>
    down in the <body>:
    HTML Code:
     <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 Code:
    <?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.

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    PHP Code:
    if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
      
    header("Refresh: 5; URL=upload.php?redirect=$redirect");
      ... 
    The variable $redirect hasn't been set.

  3. #3
    Join Date
    Aug 2006
    Location
    New York/North Carolina
    Posts
    238
    ... ... ... 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.

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    ... because upload.php probably doesn't know how to handle redirect=''

  5. #5
    Join Date
    Aug 2006
    Location
    New York/North Carolina
    Posts
    238
    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.

  6. #6
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    I've tested the redirect in "auth.inc.php" on my server and it works fine.

    Can you post upload.php?

  7. #7
    Join Date
    Aug 2006
    Location
    New York/North Carolina
    Posts
    238
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles