登录php后重定向到首页 [英] Redirect to home page after login php

查看:132
本文介绍了登录php后重定向到首页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个登录表单.如何使用户登录时重定向到显示其姓名的页面?我已经看过很多关于如何做到这一点的教程,但是没有什么是很好的...

I've created a login-form. How can I make so that when a user logs in he/she redirects to a page where it displays his/her name? I've walked through many tutorials on how to do this, but nothing was pretty good...

main_login.php:

main_login.php:

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

login_success.php:

login_success.php:

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:http://main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

checklogin.php:

checklogin.php:

<?php

$host="host"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="table"; // Table name 

// Connect to server and select databse.
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['myusername']; 
$mypassword=$_POST['mypassword']; 

// 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_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

推荐答案

该示例应该对您有所帮助,但是我建议使用mysqli及其以纯文本形式存储密码的绝妙方法.您应该考虑查找mysqli,对密码进行哈希处理,并开始熟悉php 5.4,因为此处使用的许多功能已更改且不再受支持.

This example should help you a bit, but I recommend using mysqli and its a really bad idea to store your passwords in plain text. You should consider looking up mysqli, hashing passwords, and start familiarizing yourself with php 5.4 since many of the functions used here HAVE changed and are no longer supported.

checklogin.php

checklogin.php

<?php

$host="host"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="table"; // Table name 

// Connect to server and select databse.
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['myusername']; 
$mypassword=$_POST['mypassword']; 

// Remove Magic Quotes
if (get_magic_quotes_gpc()){
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
}
$sql = sprintf(
  "SELECT * FROM $tbl_name WHERE username='%s' and password='%s' LIMIT 1;",
  mysql_real_escape_string($myusername),
  mysql_real_escape_string($mypassword)
);
$result = mysql_query($sql);

// MySQL count
$count = mysql_num_rows($result);

if ($count){
  $_SESSION['username'] = $myusername; // $_SESSION['loggedin'] = true or false would work too
  $_SESSION['mypassword'] = $mypassword; // Why store the password in session data?
  header("Location: login_success.php");
}else{
  header("Location: main_login.php?msg=Login_Failed");
}
?>

login_success.php

login_success.php

<?php
session_start();
if (!isset($_SESSION['username']) || empty($_SESSION['username']){
  header("Location: mail_login.php");
}

?>
<html>
<body>
Welcome <?php echo $_SESSION['username']; ?>
Login Successful
</body>
</html>    

main_login.php

main_login.php

<?php
if (isset($_GET['msg']) && !empty($_GET['msg']) echo $_GET['msg'];
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

使用后果自负. :)

这篇关于登录php后重定向到首页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆