stellartone Posted January 15, 2010 Share Posted January 15, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/188560-basic-php-login-system-using-php-sessions-error/ Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188560-basic-php-login-system-using-php-sessions-error/#findComment-995506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.