简体   繁体   中英

PHP check login verification

I have looked for a while to find how to fix this issue with deprecated PHP session_register and session_is_registered() functions. Here is my code i'm currently using

// 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 "home.php"
session_start();
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
//session_register("myusername");
//session_register("mypassword");
header("location:home.php");
}

Not sure if I include both of these variable or just keep my username as the main verification?

$_SESSION['myusername'] = $myusername;

$_SESSION['mypassword'] = $mypassword;

As for my main page after login. I have no idea what to put in here as in what code to use for my case.

<?
session_start();
if(empty($_SESSION["myusername"])){
header("location:index.php");
exit();
}
?>

My site is

http://pyrostudio.byethost16.com/index.php and i created account info so people can access. User is "ffffff" without "" and pass is "ffffff" without"" in fact that i don't think the session is even registering. here is the home page after login.

http://pyrostudio.byethost16.com/home.php

ignore the other errors this is to show that the session dosent even register. though i do appologize for a long question. I have not idea what to do when 5.4 came around.

You can use

<?
session_start();
if(!empty($_SESSION["myusername"]) && isset($_SESSION["myusername"])  ){
//do something
}else{
header("location:index.php");
exit();
}
?>

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