tuxbuddy Posted April 14, 2008 Share Posted April 14, 2008 I have written a login script which when user logins is redirected to new page.Here are the codes: File: login.php <html><table width="300" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <p> <body bgcolor="#d0d0d0"> <img src="helpcore.jpg" align="center"> </p> <br><iframe src="http://free.timeanddate.com/clock/iz28p1x/n438/fc969/tccff/pcff9/ftb/bas2/bat6/bacf00/tt0/td1/tb3" frameborder="1" width="326" height="22"></iframe> </br><br> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> <td><br><a href="forget.php"><i>Forgot your password?</i></a><td><a href="register.php">Register</a></td> </tr> </table> </td> </form> </tr> </table> </html> File : checklogin.php <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="mysql123"; // Mysql password $db_name="helpcore"; // Database name $tbl_name="users"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $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); $sql="SELECT * FROM $tbl_name WHERE loginname='$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:login_success.php"); } else { echo "Wrong Username or Password"; //echo "You need to register to login.Go Back and click on "Register" button."; } ?> If the login is successful,then.he is redirected to: File:login_sucess.php <? session_start(); if(session_is_registered(myusername)){ #header("location:http://10.14.2.36/HelpCORE/modules_helpcore_public"); echo "<meta http-equiv='refresh' content='0;url=http://10.14.2.36/HelpCORE'>"; } ?> <html><body> Login Successful. </body></html> Now the new page gets displayed where I want to display like say: Welcome $username And then the other content.How can I carry the variable to this new page. Pls Help Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/ Share on other sites More sharing options...
friedemann_bach Posted April 14, 2008 Share Posted April 14, 2008 Assuming that you use PHP 4.0.6 or higher, I would recommend to use $_SESSION['your_var_name'] = '...' instead of session_register(). Then use $_SESSION['your_var_name'] to refer to the session variables, like: echo "Welcome ".$_SESSION['your_var_name']; I would also recommend to integrate all login related operations into one script. There should be a number of examples available in the scripts section. Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516560 Share on other sites More sharing options...
tuxbuddy Posted April 14, 2008 Author Share Posted April 14, 2008 Lemme tel yu my problem in details: File: login.php <html><table width="300" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <p> <body bgcolor="#d0d0d0"> <img src="helpcore.jpg" align="center"> </p> <br><iframe src="http://free.timeanddate.com/clock/iz28p1x/n438/fc969/tccff/pcff9/ftb/bas2/bat6/bacf00/tt0/td1/tb3" frameborder="1" width="326" height="22"></iframe> </br><br> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> <td><br><a href="forget.php"><i>Forgot your password?</i></a><td><a href="register.php">Register</a></td> </tr> </table> </td> </form> </tr> </table> </html> File : checklogin.php Code: <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="mysql123"; // Mysql password $db_name="helpcore"; // Database name $tbl_name="users"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $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); $sql="SELECT * FROM $tbl_name WHERE loginname='$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:login_success.php"); } else { echo "Wrong Username or Password"; //echo "You need to register to login.Go Back and click on "Register" button."; } ?> If the login is successful,then.he is redirected to new page index.php through login-success.php: File:login_sucess.php Code: <? session_start(); if(session_is_registered(myusername)){ #header("location:http://10.14.2.36/HelpCORE/modules_helpcore_public"); echo "<meta http-equiv='refresh' content='0;url=http://10.14.2.36/HelpCORE'>"; } ?> <html><body> Login Successful. </body></html> Now again I wrote a page which will again redirect back to original Position like this: <? session_start(); if(session_is_registered(myusername)){ #header("location:http://10.14.2.36/HelpCORE/modules_helpcore_public"); echo "<meta http-equiv='refresh' content='0;url=http://10.14.2.36/petp/askstatus.php'>"; } ?> <html><body> Successful. </body></html> Again I am out of my tool….now I wrote File : askstatus.php <?php Welcome <? $_SESSION[‘myusername’]; ?> ?> It is not working just displaying Welcome; Any error??? Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516598 Share on other sites More sharing options...
friedemann_bach Posted April 14, 2008 Share Posted April 14, 2008 Ok. Did you already try to replace (as I proposed) session_register("myusername"); session_register("mypassword"); by $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; ? Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516600 Share on other sites More sharing options...
tuxbuddy Posted April 14, 2008 Author Share Posted April 14, 2008 I changed whatever yu specified?But what abt if(session_is_registered(myusername)){ What gonna be right entry for that??/ Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516602 Share on other sites More sharing options...
friedemann_bach Posted April 14, 2008 Share Posted April 14, 2008 Try isset ($_SESSION['myusername']) instead. Alternatively, use !empty ($_SESSION['myusername']) if you need to check whether the username contains something. Hope this helps! Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516618 Share on other sites More sharing options...
tuxbuddy Posted April 14, 2008 Author Share Posted April 14, 2008 I tried modifying like this but its not working: <? session_start(); #echo "Welcome". $_SESSION['myusername']; #if(session_is_registered(myusername)){ if(isset($_SESSION['myusername']){ #header("location:http://10.14.2.36/HelpCORE/modules_helpcore_public"); echo "<meta http-equiv='refresh' content='0;url=http://10.14.2.36/HelpCORE'>"; } ?> <html><body> Login Successful. But if (session_is_regsitered(myusename)) is working. Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516620 Share on other sites More sharing options...
tuxbuddy Posted April 14, 2008 Author Share Posted April 14, 2008 Should I start session_start() at the top of checklogin.php Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-516626 Share on other sites More sharing options...
tuxbuddy Posted April 15, 2008 Author Share Posted April 15, 2008 Thanks Buddy....Its Done. Link to comment https://forums.phpfreaks.com/topic/101014-help-with-login-script/#findComment-517432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.