简体   繁体   中英

New session login script not working in PHP

I am using this script see below, which I got from this site to login users and redirect to a webpage based on their login. It works great but I am having trouble pulling the username into the page they are redirected to. The new session script below usually works with simple scripts but it does not appear to work with this redirect script. If someone could point me in the right direction I would be grateful as I am very new to PHP, this is my first attempt. Thanks Alan

Redirect script works great.

<?php 
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="trydata"; // Database name 
$tbl_name="users"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and   password='$mypassword'"; 
$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"login_success.php" 
    $_SESSION['username'] = $myusername; 
    $_SESSION['password'] = $mypassword; 

    $result = mysql_fetch_array($result); // get the result set from the query

    $redirect = trim($result['redirect']); // get the redirect column's value

    if ($redirect == '') {
        echo "No redirect value was set!";
    } else {
        header('Location: ' . $redirect);
        exit;
    }
} else { 
    echo "Wrong Username or Password"; 
} 

?>

New session script not pulling loin information through.

<?php 
session_start(); // start up your PHP session! 


if (isset($_SESSION['username'])
{

    echo "You are logged in as ".$_SESSION['username'];
    echo "<p>";

    echo "<a href='logout.php'> Click here to logout<a/>";
    }

else

header ("Location: form.php");

?>

try using session_write_close just before redirecting:

if ($redirect == '') {
    echo "No redirect value was set!";
} else {
    session_write_close(); 
    header('Location: ' . $redirect);
    exit;
}

Make sure you set session_start(); on the top line, after the <?php line of both your scripts which access the $_SESSION array.

<?php
session_start(); <----------------Session should start here 
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="trydata"; // Database name 
$tbl_name="users"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and   password='$mypassword'"; 
$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"login_success.php" 
    $_SESSION['username'] = $myusername; 
    $_SESSION['password'] = $mypassword; 

    $result = mysql_fetch_array($result); // get the result set from the query

    $redirect = trim($result['redirect']); // get the redirect column's value

    if ($redirect == '') {
        echo "No redirect value was set!";
    } else {
        header('Location: ' . $redirect);
        exit;
    }
} else { 
    echo "Wrong Username or Password"; 
} 

?>

New session script not pulling loin information through.

<?php 



if (isset($_SESSION['username'])
{

    echo "You are logged in as ".$_SESSION['username'];
    echo "<p>";

    echo "<a href='logout.php'> Click here to logout<a/>";
    }

else

header ("Location: form.php");

?>

You need to call session_start() at the very beginning.

and I advice you to learn PDO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM