Session Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Invisible Star
Forum Newbie
Posts: 1
Joined: Wed Dec 17, 2003 11:53 am

Session Help

Post by Invisible Star »

Hi all .. I really don't know how to use session and I am not an expert in PHP I am just a beginner so I am requesting your help

I have three pages

First page = login.php << it has only the form to login
Second page = login_pro.php << to validate and check if the user is registered
Third page = mypage.php << a page for the user where s/he can update his information

where exactly should I put the code and where should I use the session_start(), is it before the <html> or after the <body> ???

Here is the code that i had but i don't know where should i put it ecaxtly and in which page :? :? :? ..

Code: Select all

<?php

session_start();

//You can relpace $s != session_id() with !session_is_registered('username') or !$_SESSION['username']
if($s != session_id() || !$action || $action == "") {
    if(!$_POST['username']) {
        $username = $_SESSION['username'];
        $password = $_SESSION['password'];
    }
    else {
        $username = $_POST['username'];
        $password = $_POST['password'];
    }
    
    @ $db = mysql_connect("localhost", "root", "") or die("Error: Could not connect to database.");
    
    mysql_select_db("Tarjama");
    
    $login_query = "SELECT * FROM customer WHERE Username='$username' AND Password='$password'";
    
    $login_result = mysql_query($login_query);
    $check = mysql_num_rows($login_result);
    
    if($check > 0) {
        if($error) {
            echo "<b>You have the following errors:</b><br>". $error; 
            $action = "not_logged";
        }
        else {
            while($row = mysql_fetch_array($login_result)) {
                session_register('username');
                $_SESSION['username'] = $row['Username'];
                session_register('password');
                $_SESSION['password'] = $row['Password'];
                session_register('user_page');
                $_SESSION['user_page'] = $row['User_Page'];
            }
            $action = "show_user_page";
        }
    }
    else {
        echo "<strong>Logon Denied</strong> Invalid username OR password.";
        $action = "not_logged";
    }                         
}

if($action == "not_logged") {
    //echo your login page here
}

if($action == "show_user_page") {
    //This is just an example path_to_pages/user_dir/user_page
    include("uploads/".$_SESSION['username']."/".$_SESSION['user_page'])
}
  
?>

I really need someone to help me..

Your help is appreciated
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

Before using a script, try to understand the code... no use in using sessions if you don't know how to implement them...

session_start() should be the first command on the page.. so before any output..
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

There's a great article on this subject on Sitepoint:
Managing Users with PHP Sessions and MySQL.
It helped me great!
Post Reply