Session login in php

<?php
ob_start();
include("db_connect.php");
$tbl_name='login';
$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);
$encrypted_mypassword = crypt($mypassword,'ctk'); 

 $sql="SELECT * FROM $tbl_name WHERE u_name='$myusername' and password='$encrypted_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){
session_register("myusername");
session_register("encrypted_mypassword");
header("location:edit-grid.php");
}
else {header("location:main_login.php?a=Login Failed Try Again!!");
//echo "Wrong Username or Password";
}
ob_end_flush();
?>

I have the login page shown above, in localhost it works fine, but it shows an error in firebug: Failed to load the source for: http: //localhost/emp_tracker/main/checklogin.php

On the server, which is located when it is hosted, it does not redirect its failure (the session ends with an error),

+3
source share
2 answers

Updated Answer

This code should work:

session_start();
include("db_connect.php");

$myusername = mysql_real_escape_string($_POST['myusername']);
$mypassword = crypt($_POST['mypassword'], 'ctk'); 
$mypassword = mysql_real_escape_string($mypassword);

$sql = "SELECT * FROM `login` WHERE `u_name` = '$myusername' and `password` = '$encrypted_mypassword'";
$result = mysql_query($sql);

if(mysql_num_rows($result) == 1)
{
    $_SESSION['myusername'] = $myusername;
    $_SESSION['encrypted_mypassword'] = $encrypted_mypassword;
    header("Location: http://servername/folder/edit-grid.php");
}
else
{
    header("Location: http://servername/folder/main_login.php?a=Login Failed Try Again!!");
}

Original answer

The line below does not make sense. You have already started a session at the beginning of your script. Also session_startdoes not accept any arguments. Removing it may solve your problem.

session_start('myusername');

, URL:

HTTP/1.1 URI ": , , URI. $_SERVER [ 'HTTP_HOST'], $_SERVER ['PHP_SELF'] dirname() URI .

: http://www.php.net/manual/en/function.header.php

Edit: script . :

$_SESSION['myusername']=$result['myusername'];

$result['myusername'];, . :

$row = mysql_fetch_assoc($result);
$_SESSION['myusername'] = $row['u_name'];
+1

, . , , 1) . . no echo html 2) , php, . 3) Google , firebug. ( ) 4) . , passowrd, . , .

500 ?

0

Source: https://habr.com/ru/post/1765762/


All Articles