Jump to content

Basic PHP login system using PHP Sessions error


stellartone

Recommended Posts

I am trying to write a login script.  I have gotten far enough to where the session starts after the database recognizes the username and password.  the error im getting is that when it confirms the password, in "checklogin.php" it redirects and the session doesnt seem to work and carry the values to the next page.

 

What am i doing wrong.

 

the form :

<form method="post" action="checklogin.php">

                 Username: 

            <input name="myusername" type="text" id="myusername" />

               Password: 

            <input name="mypassword" type="password" id="mypassword" />

              

            <input type="image" name="Submit" value="Login" src="images/arrow2b.gif" height="17" width="17" class="buttoninput" align="absmiddle" />

          </form>

 

checklogin.php

 

<?php

 

session_start();

 

 

$link = mysql_connect('date', 'user', 'pass');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

 

mysql_select_db(constdb, $link)or die("cannot select DB"); // select the database

 

 

 

$myusername=$_POST['username'];

$mypassword=$_POST['password'];

 

// 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 users 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: member/index.php");

 

}

 

else {

 

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=www.disney.com">';   

}

?>

 

 

Top of the members/index.php page:

 

<?php

session_start();

if(!isset($_SESSION['valid_user']))

{

header ("location: http://www.guconstruction.net");

}

?>

 

 

 

WHATS GOING ON HERE?

Link to comment
Share on other sites

Your code relies upon session_register, a function that has been deprecated for over 9 years. The proper format is $_SESSION['key'] = 'value', this easy fix should rectify your problems.

 

An old tutorial you followed?

 

And where is $_SESSION['valid_user'] defined in the login code? If it's never defined, it will always redirect because the key simply doesn't exist.

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.