Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Sessions are not registering.

    PHP Code:
    <?php

    // Last Modified 16th August

    ob_start();

    include(
    'templates/header.php'); // Header template
    include('../config/config.php'); // Includes configuration files
    include('../lib/functions.php'); // Load all functions from Library

    include('templates/login.php'); // Login form

    $submit $_POST['log_in'];
    $posted_username sql_safe($_POST['username']);
    $posted_password sql_safe($_POST['password']);

    if(isset(
    $submit)){

    if(
    $posted_username == $username && $posted_password == $password){
        echo 
    "Logged in";
        
    $_SESSION["username"];
        
    $_SESSION["password"];
    }
    else {
        echo 
    "Wrong username or password";
    }

    }

    include(
    'templates/footer.php'); // Footer Template

    ?>
    Sessions are not registering for me. An example of sessions being used at are:
    PHP Code:
    <?php

    // Last Modified 14th August 2007
    ob_start();
    session_start(); // Loads session

    include('templates/header.php'); // Header Template
    include('../config/config.php'); // Includes configuration files
    include('../lib/functions.php'); // Load all functions from Library

    if(!isset($_SESSION["username"])){
    header('Location: index.php');
    exit();
    }
    else {

    include(
    'templates/candidates.php'); // Add new candidates template

    $submit $_POST['submit']; // Submit button
    $candidate sql_safe($_POST['name']); // New candidate's name

    if(isset($submit)){

    mysql_query("INSERT INTO candidates(name) VALUES('$candidate')") or die(mysql_error());

    $newly_created_candidate mysql_query("SELECT * FROM candidates ORDER BY `id` desc LIMIT 1");
    while (
    $row mysql_fetch_array($newly_created_candidate)){
    $candidateID $row['id'];
    }

    $result mysql_query("SELECT id FROM questions");
    while (
    $row mysql_fetch_array($resultMYSQL_NUM)) {
    $qid=$row[0];
    $query "INSERT INTO answers(answer,candidate_id,question_id) VALUES('1','$candidateID','$qid')";
    mysql_query($query)or die(mysql_error());
    }

    }

    $sql mysql_query("SELECT * FROM candidates");
    while(
    $row mysql_fetch_array($sql)){
        echo 
    $row['name'];
        echo 
    "<br />";
        
    $count++;
    }

    echo 
    '<br /><b>There are '$count.' Candidates</b>';

    mysql_close(); // Closes DB connection
    }

    include(
    'templates/footer.php'); // Footer Template

    ?>
    Any ideas, what am I doing wrong?

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You never used session_start() in the first code, and I don't know what these lines are supposed to be doing:
    PHP Code:
    $_SESSION["username"];
    $_SESSION["password"]; 

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by PappaJohn View Post
    You never used session_start() in the first code, and I don't know what these lines are supposed to be doing:
    PHP Code:
    $_SESSION["username"];
    $_SESSION["password"]; 

    Where do I start the session? It doesn't work.

    PHP Code:
    $_SESSION["username"];
    $_SESSION["password"]; 
    These lines are suppose to be registering sessions?

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    session_start() should be at the top of the first script, like it is in the second.

    In order to set session variables, you have to set them to something - like this:
    PHP Code:
    $_SESSION['username'] = 'my name';
    $_SESSION['password'] = 'my password'
    this has to be done after session_start()

    On a side note, it's probably not a good idea to be storing passwords in sessions variables.

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by PappaJohn View Post
    session_start() should be at the top of the first script, like it is in the second.

    In order to set session variables, you have to set them to something - like this:
    PHP Code:
    $_SESSION['username'] = 'my name';
    $_SESSION['password'] = 'my password'
    this has to be done after session_start()

    On a side note, it's probably not a good idea to be storing passwords in sessions variables.
    I can store them in a database but the script only needs basic functions to login.

    Thanks anyways, fixed.
    Last edited by MHaris; 08-17-2007 at 02:02 AM.


  •  

    Posting Permissions

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