Jump to content

PHP Sessions


docpepper

Recommended Posts

Hi all,

 

I have just moved server and am now on php 5.1.2. This is causing issues with a login script I am using, which used session_register etc.

 

My current code for the checklogin.php page is:

 

<?php
ob_start();
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db_name"; // Database name
$tbl_name="tbl_name"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$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);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Check Login Success</title>
<link href="style/login.css"type=text/css rel="StyleSheet" />
</head>
<body>
<div class="login">
<h1>Welcome <?php echo ($myusername); ?></h1><br />
<form name="form2" method="post" action="Logout.php">
<div class="submit_wrap"><a href="Logout.php" title="Logout">
<input name="Logout" value="Logout" type="submit" title="Logout" id="logout"></a></div>
</form>
</div>
</body>
</html>
<?php
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Check Login Error</title>
</head>
<body>
<div class="login">
<h1>Wrong Username or Password</h1>
<form name="form1" method="post" action="checklogin.php">
Username: <input name="myusername" type="text" id="myusername" />
Password: <input name="mypassword" type="password" id="mypassword" />
<div class="submit_wrap"><input name="Login" value="Login" type="submit" title="Login" id="submit" /></div>
</form>
</div>
</body>
</html>
<?php
}
ob_end_flush();
?>

 

And my Session start page is:

 

<?php
session_start();
if (isset($_SESSION['myusername'])){
?>
You have Logged in!!
<?php
} else {
header("location:/index.php");
} ?>

 

Now I know exactly where the issue lies, it is to do with the session_register('my_username'); needing to be changed to $_SESSION...

 

But my only issue is i'm not sure how and more importantly where to change this in the current script.

 

So my question is how would I change the session_register call to $_SESSION and where would I put it?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/96681-php-sessions/
Share on other sites

Fantastic cheers peeps sorted my issue out straight away..

 

Funnily enough I actually tried

 

if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;

 

But it was throwing me back to the login page...Must of been the session_start() issue aswell!

 

Excellent work thankyou :)

Link to comment
https://forums.phpfreaks.com/topic/96681-php-sessions/#findComment-494750
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.