SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Setting a SESSION to a link?

    If I have a page with a menu containing: START - PAGE A - PAGE B - PAGE C.

    Is it possible to make the link just update the current page (index.php) and set a session when someone click on a specific link?
    Let's say they press PAGE1, then I want the same page to reload but set $_SESSION["menu"] = "A".

    That would be a great thig, cause then I would be able to only have on page on my site and just make a couple of IF THEN on the page to show whatever is set in the Session value.

    Can someone tell me how to do this or if it's possible?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You don't need to create that session mess to have only one file controlling your page. Just link to the same page with a parameter in the URL (you'd need to do this anyway):

    index.php?page=a
    index.php?page=b
    index.php?page=c

    And your condition tests $_GET['page'].

    You can make nicer URLs with Apache's mod_rewrite, and still use a single file to do the work.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But isn't there a way to set a session within the link instead? Would look sooooo great and clean.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think I managed to do this myself. I did an IF statement in the top to check if the SESSION was set and then reloaded the page. It seems to work fine now.

  5. #5
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. There is no way that you can start a session or modify the session (eg. headers) after any content has been parsed.

    This means, once you export any HTML you can no longer modify the headers for that page. That includes the session. Therefore, there would be no way for you to edit the session on that page directly. The only way is it the link went to page.php?a=1 or something, and then when the page reloaded it set the variable as a session. It would look like this:
    PHP Code:
    <?
    session_start
    ();

    $_SESSION["menu"] = $_GET['a'];

    Echo
    "<a href='page.php?a=1'>Link</a> | <a href='page.php?a=2'>Link</a> | <a href='page.php?a=3'>Link</a>";

    if(!isset(
    $_SESSION["menu"])){
      include();
    }elseif(
    $_SESSION["menu"] =="1"){
      include();
    }elseif(
    $_SESSION["menu"] =="2"){
      include();
    }elseif(
    $_SESSION["menu"] =="3"){
      include();
    }else{
      die(
    "Error");
    }
    ?>
    However, how most websites would do it would be the same thing, just without the session:
    PHP Code:
    <?
    $a 
    $_GET['a'];

    if(!isset(
    $a)){
      include();
    }elseif(
    $a =="1"){
      include();
    }elseif(
    $a =="2"){
      include();
    }elseif(
    $a =="3"){
      include();
    }else{
      die(
    "Error");
    }
    ?>
    Best of luck.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You realize your site won't show up in search engines since spiders don't accept and resend session cookies?

    Whatever cleanliness of URLs you're looking for can be done without sessions, and must be done without sessions if you want it to actually work for everyone, including search engines


Bookmarks

Posting Permissions

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