Web Design Forum  
 
Go Back   Web Design Forum > Web Software > Programming

Like Tree1Likes
  • 1 Post By Backseat

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2011, 05:56 AM   #1
New Member
 
Join Date: Dec 2011
Posts: 15
Default PHP still slightly off

My PHP to login is still slightly messed up after I made some modifications

error message after I try to login: "Notice: Undefined index: act in C:\wamp\www\youtube\login.php on line 65"

line 65: $act = $_REQUEST['act'];

Thanks a lot, I really appreciate any help.. I've been trying to figure this out for very long now

Code:
<?php 
session_start(); 

//This displays your login form 
function index(){ 
echo "<form action='?act=login' method='post'>"  
."Username: <input type='text' name='username' size='30'><br>" 
."Password: <input type='password' name='password' size='30'><br>" 
."<input type='submit' value='Login'>" 
."</form>";  
} 

//This function will find and checks if your data is correct 
function login(){ 

//Collect your info from login form 
$username = $_REQUEST['username']; 
$password = $_REQUEST['password']; 


//Connecting to database 
$connect = mysql_connect("localhost:3306","root","1234"); 
if(!$connect){ 
die(mysql_error()); 
} 

//Selecting database 
$select_db = mysql_select_db("youtube", $connect); 
if(!$select_db){ 
die(mysql_error()); 
} 

//Find if entered data is correct 
$result = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'"); 
$row = mysql_fetch_array($result); 
$id = $row['id']; 

$select_user = mysql_query("SELECT * FROM user WHERE id='$id'"); 
$row4 = mysql_fetch_array($select_user); 
$user = $row4['username']; 

if($username != $user){ 
die("Username is wrong!"); 
} 

$pass_check = mysql_query("SELECT * FROM user WHERE username='$username' AND id='$id'"); 
$row3 = mysql_fetch_array($pass_check); 
$email = $row3['email']; 
$select_pass = mysql_query("SELECT * FROM user WHERE username='$username' AND id='$id' AND email='$email'"); 
$row5 = mysql_fetch_array($select_pass); 
$real_password = $row5['password']; 

if($password != $real_password){ 
die("Your password is wrong!"); 
} 

//Now if everything is correct let's finish his/her/its login 

session_register("username", $username); 
session_register("password", $password); 

echo "Welcome, ".$username." please continue on our <a href=index.php>Index</a>"; 

} 
$act = $_REQUEST['act'];
switch($act)
{ 
default:
index(); 
break; 

case "login":
login(); 
break; 
} 
?>
Alex is offline   Reply With Quote


Old 12-17-2011, 03:12 PM   #2
Silver Member
 
Join Date: Jul 2011
Posts: 146
Default

Alex, if your script is executed without 'act' being present in the query string, you will get that error. So I suspect what you want to do is change this:
Code:
$act = $_REQUEST['act'];
switch($act)
{ 
default:
index(); 
break; 

case "login":
login(); 
break; 
}
To this:
Code:
if (isset($_REQUEST['act'])) {
    if ($_REQUEST['act'] == 'login') login();
    else index();
    }
else
    index();
Bill
__________________
space
Orchid Technical Services
Website Design/Development and Technical Support
DesignerBill is offline   Reply With Quote
Old 12-17-2011, 05:47 PM   #3
New Member
 
Join Date: Dec 2011
Posts: 15
Default

Thanks for taking the time to go through the code, still doesn't seem to work though. I guess I'll scrap the code and try something new. lol PHP is so annoying
Alex is offline   Reply With Quote
Old 01-06-2012, 09:21 AM   #4
New Member
 
Join Date: Jan 2012
Posts: 12
Default

Hey

I'm not going to reply to your actual question here, sorry for that. I'm going to try to help you on your way though with a few pointers.

build your script step by step
-> don't make a login thing all at once, just try toying with session parameters. When that works, try connecting to a database and retrieving something, then try getting something fromt he db and putting it in the session, when all of that works, change it to become a login system, when THAT works, go to the next step etc.

This will make it easier to master all techniques. If you only change 2 things and your code breaks, you will know the mistake is somewhere in those 2 things. This will make it much easier for you to find mistakes, so work with small edit/test cycles.

Also, don't use the switch mechanism with the act thing. Why not use two pages and have one page post to the other?
- It clears you of the switch(act) issue
- It looks a lot better
- you can just write most of the HTML outside of your code brackets, which is cleaner
- you don't get a 5km file to scroll through

Just some suggestions to make your life easier, hope that it didn't brush you the wrong way. I respect you learning this on your own and am just trying to give you some pointers.
DesignerBill likes this.
Backseat is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:49 AM.


Camera Forum - Computer Forum - Web Design Forum

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.