How do sessions work? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do sessions work?

tux55tux55 Posts: 21Member
Before I start rambling, I'm running an OpenBSD web server using Apache and PHP 4.

All I want is a login system where the users type in any name they want and a pre-specified password in "index.php" and it takes them to "terminal.php" and uses their user name that they specified in that page. However, it shouldn't take them to the terminal.php if the password is wrong and they shouldn't be able to access terminal.php without logging in first. I figured that the easiest way to do this is with php sessions, but so far no dice.

It seems to log in correctly, but the terminal page is blank. Please excuse my messy and amature coding! I would *really* appreciate if someone would sift through it with me and tell me what I'm messing up on. Thanks!

# -----LOGIN.PHP : WHERE INDEX GOES TO VALIDATE LOGIN

Page Title
<?php
if($_POST['pass'] == 'thepassword'){
session_start();
$key = "3hfdy";
session_register($key);
session_name = ($_POST['name']);
echo 'Login good, loading...';
header('Location:terminal.php');
}
else {
echo "INCORRECT LOGIN";
}
?>


# -----TERMINAL.PHP : CHECKS FOR VALID LOGIN OR WON'T DISPLAY PAGE

<?php
session_start();
if($key != '3hfdy'){
echo 'LOGIN INVALID';
}
else { ?>

Page Title


Welcome, <?php echo (session_name); ?>
(and the real content goes here)


Comments

  • pritaeaspritaeas Posts: 274Member
    [b][red]This message was edited by pritaeas at 2006-9-1 9:33:9[/red][/b][hr]
    [b][red]This message was edited by pritaeas at 2006-9-1 9:32:30[/red][/b][hr]
    I've found the best way to use session is to start every file with:

    [code]
    <?php
    if (! session_id ()) session_start ();
    ?>
    [/code]

    I've had trouble on some platforms, where info got lost if I didn't do this first.

    Also, instead of using session_register, you can use:

    [code]
    $_SESSION['myvariablename'] = 'myvalue';
    [/code]

    Should you want to destroy your session, use:

    [code]
    session_unset ();
    session_destroy ();
    [/code]

    h.t.h. Hans


  • mac_doggiemac_doggie Posts: 488Member
    just alway's start your file with session_start(); Don't try to execute this function after the headers are already sent... In your example the headers are already sent, because you started the output by typing html...
    My advice, first build your page and do all coding you want, and only do one echo at the end of your file... saves a lot of frustrations with headers allready sent errors...

    When I started PHP I did it the same way you do, html and code in one file, but while I gained more experience I found out this was unmaintainable...(is that a real word..? :-) )

    Then I started to use a variable throughout the whole file that I echoed at the end, so the output always started at the end of my script...

    Now I use my own build templatesystem wich is a far better way I think, but that would ben to difficult for you at this stage...

    I guess you should try the variable way...

    like:
    [code]

    $output = "


    title

    ";

    // do all kinds of things...
    // add some more output

    $output .= "


    ...etc...


    ";


    // finaly parse the whole...
    echo $output;


    [/code]

    ofcourse you should create some nice functions around it and at one time you will find yourself creating a class around it and calling it Template... :-)

    have fun...

    : [b][red]This message was edited by pritaeas at 2006-9-1 9:33:9[/red][/b][hr]
    : [b][red]This message was edited by pritaeas at 2006-9-1 9:32:30[/red][/b][hr]
    : I've found the best way to use session is to start every file with:
    :
    : [code]
    : <?php
    : if (! session_id ()) session_start ();
    : ?>
    : [/code]
    :
    : I've had trouble on some platforms, where info got lost if I didn't do this first.
    :
    : Also, instead of using session_register, you can use:
    :
    : [code]
    : $_SESSION['myvariablename'] = 'myvalue';
    : [/code]
    :
    : Should you want to destroy your session, use:
    :
    : [code]
    : session_unset ();
    : session_destroy ();
    : [/code]
    :
    : h.t.h. Hans
    :
    :
    :

    ;-)
    -mac-
    mailto:[email protected]
    the Netherlands...


Sign In or Register to comment.