English 中文(简体)
未在购买力平价中工作的新的会议记录
原标题:New session login script not working in PHP
  • 时间:2012-05-11 08:04:14
  •  标签:
  • 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

转稿很有价值。

<?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");

?>
问题回答

确保在<代码>和>后,在顶线上确定会议开始;在<代码>后,确定网站 和 两个字面上,可登录_SESSION阵列。

<?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");

?>

您从一开始就需要打上<代码>session_start(start)。

http://www.phpeveryday.com/articles/PDO-Prepared-Statement-P550.html





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签