Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,199,959 members, 7,973,312 topics. Date: Saturday, 12 October 2024 at 01:47 PM |
Nairaland Forum / Science/Technology / Programming / PHP Programmers Please Help With This Code (1135 Views)
Programmers, Please Recommend A Good Laptop For Me. / Abuja Php Programmers Where Are You? I Have A Job For You / Need Professional Php Programmers For A Project, Money Involved (2) (3) (4)
PHP Programmers Please Help With This Code by WebsiteGuy: 8:16pm On Apr 28, 2017 |
Please what is wrong with this code? ----------------------- <?php if (isset($_POST['submit'])){ include("database.php" $myusername = $_POST["username"]; $mypass = $_POST["pass"]; $pincodequery = "Select * from login where username ='$myusername' AND pass ='$mypass'"; $pinquery = mysql_query($pincodequery); $run = mysql_num_rows($pinquery); if ($run > 0 ) { header("Location: Home.php" } else { echo"<script>alert('Incorrect Username or Password')</script>"; echo"<script>window.open('cpanel/login.html', '_self')</script>"; } } ?> --------------------------------------------- It is the action script on login submit button. After inputing correct username and password it goes to "cpanel/login.html" instead of "Home.php" What could be wrong with the code? |
Re: PHP Programmers Please Help With This Code by hexy40: 8:33pm On Apr 28, 2017 |
You know if there is ano extra space in front or after the name or password $run will return 0. So I suggest u take a good look at the user name and password, trim the username variable. |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 8:45pm On Apr 28, 2017 |
Thanks @ hexy40; I have cleaned as advised but still the same error. Below is the new code -------------------------------- <?php if (isset($_POST['submit'])){ include("database.php" $myusername=$_POST["username"]; $mypass=$_POST["pass"]; $pincodequery = "Select * from login where username='$myusername' AND pass='$mypass'"; $pinquery = mysql_query($pincodequery); $run = mysql_num_rows($pinquery); if ($run > 0 ) { header("Location: Home.php" } else { echo"<script>alert('Incorrect Username or Password')</script>"; echo"<script>window.open('cpanel/login.html', '_self')</script>"; } } ?> |
Re: PHP Programmers Please Help With This Code by hexy40: 8:55pm On Apr 28, 2017 |
I cant really help from here except if its not a sensitive project and I can see the data base and structure so as to trace the problem. If its a database you are just using for practice, export and post the database here in sql form. |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 9:07pm On Apr 28, 2017 |
Thanks so much hex40; I think the problem is from the home.php because it is not bringing up the alert message; help me point out what is wrong please: ------------------------------------- <?php if (!isset($_SESSION['usernam'])) { header("location: cpanel/login.html" } session_start(); $myusername = $_SESSION["usernam"]; $mypass = $_SESSION["passwd"]; include('includes/header.php');?> Welcome to admin</title> <style type="text/css"> #apDiv1 { position: absolute; width: 585px; height: 25px; z-index: 1; left: 722px; top: 181px; } </style> </head> <body> <div id = "mainBody"> |
Re: PHP Programmers Please Help With This Code by hexy40: 9:24pm On Apr 28, 2017 |
From what am seeing: If $run>0; redirect to home page, then Immediately you got to the home page you redirected to login.html if session was not set. Now you should do is set session if $run>0 before going to the home page or else your first line of code in the home page "<?php if (!isset($_SESSION['usernam'])) { header("location: cpanel/login.html" ); }" will send it back to login.html; |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 9:35pm On Apr 28, 2017 |
Please how should the code be? I will appreciate if you can post the exact code for me to use. I am not a php pro like you sir... |
Re: PHP Programmers Please Help With This Code by hexy40: 9:52pm On Apr 28, 2017 |
There are many ways you can handle this issue but the shortest I can think of right now is <?php if (isset($_POST['submit'])){ include("database.php" $myusername=$_POST["username"]; $mypass=$_POST["pass"]; $pincodequery = "Select * from login where username='$myusername' AND pass='$mypass'"; $pinquery = mysql_query($pincodequery); $run = mysql_num_rows($pinquery); if ($run > 0 ) { $_SESSION["usernam"]=$myusername; $_SESSION["passwd"]=$mypass; header("Location: Home.php" } else { echo"<script>alert('Incorrect Username or Password')</script>"; echo"<script>window.open('cpanel/login.html', '_self')</script>"; } } ?> This is your code, i just added 2 lines of code. |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 10:04pm On Apr 28, 2017 |
Still not working please check the home.php code also --------------------------- <?php if (!isset($_SESSION['usernam'])) { header("location: cpanel/login.html" } session_start(); $myusername = $_SESSION["usernam"]; $mypass = $_SESSION["passwd"]; include('includes/header.php');?> Welcome to admin</title> <style type="text/css"> #apDiv1 { position: absolute; width: 585px; height: 25px; z-index: 1; left: 722px; top: 181px; } </style> </head> <body> <div id = "mainBody"> |
Re: PHP Programmers Please Help With This Code by hexy40: 10:20pm On Apr 28, 2017 |
ok... we need to be sure that the problem is from the home page. try and tell me what happened <?php if (isset($_POST['submit'])){ include("database.php" $myusername=$_POST["username"]; $mypass=$_POST["pass"]; $pincodequery = "Select * from login where username='$myusername' AND pass='$mypass'"; $pinquery = mysql_query($pincodequery); $run = mysql_num_rows($pinquery); if ($run > 0 ) { echo " The problem is not form the database"; echo "run =".$run; } else { echo"<script>alert('Incorrect Username or Password')</script>"; echo"<script>window.open('cpanel/login.html', '_self')</script>"; } } ?> |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 10:28pm On Apr 28, 2017 |
Yes I recieved "The problem is not from the database" error |
Re: PHP Programmers Please Help With This Code by ANTONINEUTRON(m): 10:36pm On Apr 28, 2017 |
WebsiteGuy:Hmmm session_start(); should come before the if() i.e it should be at the first line |
Re: PHP Programmers Please Help With This Code by hexy40: 10:42pm On Apr 28, 2017 |
one thing I forgot to tell you is that session start should be the first thing in the beging of a page. <?php ob_start(); session_start();?> even before connection and any thing. That might not be the cause of this problem, give me some time let me test the code by myself |
Re: PHP Programmers Please Help With This Code by hexy40: 10:53pm On Apr 28, 2017 |
its working fine now... just put the session start. |
Re: PHP Programmers Please Help With This Code by WebsiteGuy: 11:27pm On Apr 28, 2017 |
Thanks so much I really appreciate. It has worked. I had to put session_start() at the top of the two pages. Thanks |
(1) (Reply)
Introducing HR Master, (automated solution for recruitment processes) / For Sale: Ecommerce Website With Payment Api / Vacancy:: Programming Job Opportunity
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 36 |