www.webdeveloper.com
Results 1 to 11 of 11

Thread: Existing Session?

  1. #1
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063

    Existing Session?

    I've been doing some testing with PHP sessions. I've got a page set up where if there is no session, it automatically redirects you to my homepage. However, it does that every time. I can't figure out how to send the set session from the form to that page in order to show it that it has been set. How would I do this?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Home page:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['foobar'] = true;
    // rest of page . . .
    ?>
    Special page:
    PHP Code:
    <?php
    session_start
    ();
    if(empty(
    $_SESSION['foobar']) or $_SESSION['foobar'] != true)
    {
       
    header('Location: http://www.example.com/index.php');
       exit;
    }
    // rest of page . . .
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    But I only want the session to be set if the form is submitted. I don't want it set as soon as a user goes to the homepage.

  4. #4
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    ((sorry for the double post; can't edit the last one at this point))

    I was able to figure my last question out on my own. But for some reason, in the test page for the session, none of the CSS is working. What's up with that?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    No way to tell without seeing it. Might be a good idea to start by running the page through the HTML validator and CSS Validator pages.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
    <html>
    <head>
    <title>Session Test Form</title>
    <LINK href="css/general.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <form id="test" name="test" method="post" action=" ">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" cols="40"> <br>
    <label for="password">Password:</label>
    <input type="text" id="password" name="password" cols="40"> <br> <br>
    <input type="submit" id="submit" name="submit" value="Submit Form"><img src="spacer.gif" width="5" height="1" alt="Submit">
    <input type="reset" id="reset" name="reset" value="Clear Form">
    </form>
    </body>
    </html>
    That's the form page to create the session, and it's doing the same thing. I have the background color in the CSS set to #000000 (black). That stylesheet works on all of my other pages that I use it on, so I don't understand why it doesn't work on this one.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Is it in the same directory as the other files that work? I ask because you use a relative path to the CSS file in your link tag.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Mar 2006
    Posts
    413
    edit: sorry didn't realise you answered your last question

  9. #9
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    It's not in the same directory as the form and session test page that I'm experimenting with. It's in my CSS directory (where I keep all of my stylesheets). To get to the pages I'm testing, you have to go to domain.com/news/page.php. The CSS directory is in the main site root.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Make the href value an absolute site path by prepending a "/", then it won't matter where the page using it is in the directory structure:
    Code:
    <LINK href="/css/general.css" rel="stylesheet" type="text/css">
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    That did it. Thanks!

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