Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - How to use session inside webpages?

I am trouble with session; I know it theoretically but I encountered with it how to use session , transferring username to other page and adding logout in order to clear the current logged account information. Since I have no idea of session usage I commented it.

This is the checklogin.php

 <?php
    session_start();
    include("database.php");

    // username and password sent from form
    $name=$_POST['email'];
    $password=$_POST['pwd'];
    //select data from database

    $sql="SELECT * FROM $tbl_name WHERE usermail='$name' && userpasswd='$password'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row

    if( $count == 1) {
        // Register $myusername, $mypassword and redirect to file "search.php"
        //session_register("$name");
        //session_register("$password");
        //$_SESSION['name']= $name;
        header("location:jcte/index.php");
    } else {
        $msg = "Wrong Username or Password. Please retry";
        header("location:ErrorPage.html");      
    }
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After the line

$result=mysql_query($sql);

add

if ($data = mysql_fetch_array($result)) {
    $_SESSION['user'] = $data['usermail'];
}

Now session created.Call this session in jcte/index.php page as:

<?php
session_start();
echo "welcome $_SESSION['user']";
?>

Unset the session in logout.php page as:

<?php
session_start();
unset($_SESSION['user']);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...