duanmu8911
2013-12-28 18:23 阅读 35
已采纳

Login.php没有重定向

On this login page when I try to login it gives a blank page. Though login is successful as on clicking the profile tab the dashboard.php is visible. Also this whole thing works just fine in my WAMP server.

What is causing this issue?

<?php

ob_start();
$host="localhost"; // Host name 
$username="######"; // Mysql username 
$password="######"; // Mysql password 
$db_name="devspan_Users"; // Database name 
$tbl_name="Members"; // Table name 

// Connect to server and select databse.
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_query("CREATE DATABASE IF NOT EXISTS ".$db_name);
mysql_select_db("$db_name",$con)or die("cannot select DB"); 



if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$tbl_name."'",$con))==0)
{
$sql="CREATE TABLE Members(
id int(4) NOT NULL auto_increment,
username varchar(65) NOT NULL default '',
password varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
)";
mysql_query($sql,$con);
}

// Define $myusername and $mypassword 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect 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,$con);

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

// If result matched $myusername and $mypassword
if($count==1){
// Register 
session_start();
$_SESSION["myusername"]=$myusername;
$_SESSION["mypassword"]=$mypassword; 
header("location:dashboard.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doubijiao2094 doubijiao2094 2014-01-01 08:02

    Update: The Problem was with my hosting not my code. sql queries took a lifetime to fetch data, hence the page did not load. Solution: Changed my Hosting service.

    点赞 评论 复制链接分享
  • duanbai1370 duanbai1370 2013-12-28 18:37

    In this block of code:

    // If result matched $myusername and $mypassword
    if($count==1){
    // Register 
    session_start();
    $_SESSION["myusername"]=$myusername;
    $_SESSION["mypassword"]=$mypassword; 
    header("location:dashboard.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    ?>'
    

    What is ?>' at the end for? Just remove that & try again. Also, note I changed the header line as part of the cleanup to be header('Location: dashboard.php'); Here it is cleaned up.

    ob_start();
    
    $host="localhost"; // Host name 
    $username="######"; // Mysql username 
    $password="######"; // Mysql password 
    $db_name="devspan_Users"; // Database name 
    $tbl_name="Members"; // Table name 
    
    // Connect to server and select databse.
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
    
    mysql_query("CREATE DATABASE IF NOT EXISTS " . $db_name);
    mysql_select_db("$db_name", $con) or die("cannot select DB"); 
    
    if (mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . $tbl_name . "'",$con))= = 0) {
      $sql = "CREATE TABLE"
           . " Members(id int(4) NOT NULL auto_increment,"
           . " username varchar(65) NOT NULL default '',"
           . " password varchar(65) NOT NULL default '',"
           . " PRIMARY KEY (`id`)"
           . ")"
           ;
      mysql_query($sql,$con);
    }
    
    // Define $myusername and $mypassword 
    $myusername=$_POST['username']; 
    $mypassword=$_POST['password']; 
    
    // To protect 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, $con);
    
    // Mysql_num_row is counting table row
    $count = mysql_num_rows($result);
    
    // If result matched $myusername and $mypassword
    if ($count == 1){
      // Register 
      session_start();
      $_SESSION["myusername"] = $myusername;
      $_SESSION["my password"] = $mypassword; 
      header('Location: dashboard.php');
    }
    else {
      echo "Wrong Username or Password";
    }
    ob_end_flush();
    
    点赞 评论 复制链接分享

相关推荐