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
Share on other sites

change this bit

 

if($count==1){
session_register("myusername");
session_register("mypassword"); 

 

to

 

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

 

and as Conker says use session_start() on all pages

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.