Jump to content


Photo

Noobie Stuff: Very Basics Explained Easily


  • Please log in to reply
117 replies to this topic

#21 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 12:33 PM

ok i would like to know how to do the above-but this is more important now:

i have these three files-index.php, login.php logged_in.php-and a databse with a table called users and fields: id (auto_increment), first_name, last_name, username, password, mail.


index.php:

<?php
//Title//
echo '<center>Game Testing</center><br><br><br>';
//Register Form//
echo 'Register';
echo '<form action="register.php" method="post">';
echo 'First Name:<input type="text" name="first_name"><br>';
echo 'Last Name:<input type="text" name="last_name"><br>';
echo 'Username:<input type="text" name="username"><br>';
echo 'Password:<input type="password" name="password"><br>';
echo 'Confirm Password:<input type="password" name="password2"><br>';
echo 'E-Mail:<input type="text" name="mail"><br>';
echo 'Confirm E-Mail:<input type="text" name="mail2"><br>';
echo '<input type="Submit" value="Register Me"></form><br><br><br>';

//Login Form//
echo 'Login';
echo '<form action="login.php" method="post">';
echo 'Username:<input type="text" name="username2"><br>';
echo 'Password:<input type="password" name="password2"><br>';
echo '<input type="submit" value="Log In"></form>';

?>

login.php:

<?php

include ("dbinfo.inc.php");

$username=$_POST['username2'];
$password=$_POST['password2'];

$query_1="SELECT id FROM users WHERE username=='$username' & password=='$password'";
$result_1=mysql_query($query_1) or die(mysql_error());
$num=mysql_num_rows($result_1); // returns numbers of matches found.

if ($num_1===0) {
    echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
    } else {
		
		$query_2="SELECT id FROM users WHERE username='$username'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_num_rows($result_2); // returns numbers of matches found.

		echo "$id";
		session_start();
		$_session['$id']='$username';
};		
?>

logged_in.php:

<?php

session_start();

echo 'Hello '.$_SESSION['$id'];

?>


In The Table Fields In The Table "users" I Have (In This Order-id, first_name, last_name, username, password, mail,) Adam, Trudgeon, Pudgemeister, login_test, myemail@adress.com.

now when i log in using ANYTHING as a username and password, it takes me to login.php and displays all this:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=='gfdsgsgs' & password=='sgdsdgfsgfd'' at line 1"

and when i use my normal username and pasword it still displays this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=='Pudgemeister' & password=='login_test'' at line 1.

I also know there are more errors in the code as before i did something that i cant remember, it logged you in no matter what your usrname and password was but with a WARNING error on the page (sometimes 2)

anyone know the prob(s) i have got here?

Cheers

Pudgemeister.

P.S. try and keep things simple-i made this topic to help noobs like me-things need to be explained with simple lang-nuthin to technical lol.
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#22 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 12:42 PM

Don't use the '==' comparison operator in MySQL.  Replace those with a single equal sign.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#23 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 01:34 PM

ok now i get this on login.php:

1
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/pudgesuk/public_html/game_testing/login.php:20) in /home/pudgesuk/public_html/game_testing/login.php on line 21

at least its echo'd the id but i dont know whats going on now-something wrong with the sql? i cant see what.

help please

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#24 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:07 PM

please i need help people
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#25 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:11 PM

echo 'Hello '.$_SESSION['$id'];
take that out.

Pull session_start();
up to the VERY top of the page right after the
<?php

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#26 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:12 PM

OMG OF COURSE!

i can't believe i missed that :|

cheers il have a test now...
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#27 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:25 PM

still same error unfortunately.

nethin else i can do?
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#28 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:29 PM

f ($num_1===0) {
    echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
    } else {
		
		$query_2="SELECT id FROM users WHERE username='$username'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_num_rows($result_2); // returns numbers of matches found.

		echo "$id";
		session_start();
		$_session['$id']='$username';
};		
?>
why do I see a session start popped in the middle of all that code??

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#29 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:30 PM

also you don't call a session variable like that it's
$_SESSION['whatever'] = "whatever";
you have your session in all lowercase.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#30 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:31 PM

ah-gd point. (hehehehehe)

il have a go fixing that but as ive only just lernt sessions im not sure exactly where to put the session code.

help? if u can?

cheers

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#31 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:31 PM

echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
also you should give your coding better form.
echo "Your Password And/Or Username Are Not Correct-Please Try Again <a href=\"index.php\">Here</a>";
or something similar, you don't try and put strings together or do string manipulation with single quotes, when you get to more advanced code  you will regret that habit.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#32 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:34 PM

ok thanks for that-ne info on the sessions part?

i understand sessions partly but i obviosly havent done it right-where would the first session start-and how would i i would like the session to actually be with the id and username. $_SESSION['$id] = $username if possible
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#33 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:38 PM

session_start();
at the top of EVERY page used with sessions.

Inside the php.ini

Session]
session.save_handler

should be set to files unless your doing custom handling
session name should be set to sessid and left at default unless you know what your doing, use only cookies is safer.  if you change the max lifetime of the session cookie to a specific number of seconds, If you search on google they have a system to calculate second just put like
number of seconds in 4 hours or something, it'll auto calculate that for you.
My standard is 4 hours, it's fair to the person,a nd they can do what they need to do before it kicks them out, don't forget to hash your passwords.

session.cookie_lifetime   = 14400
that will set you to 4 hours, you can double that for 8 hours, not recommended or safe, or you can lower it in quarters to recieve hourly.  25 percent of that is 1 hour, every other 25 percent of that is an extra hour.
Or you can auto-calculate them in google as I said.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#34 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:42 PM

example
You have a register form
where they sign up
don't forget on the password to put

$passhash = md5($password);
or something similar
Then when your ready to test, do this.
$password = md5($password);
// this is going to check the password they typed during login against the hash of the password in the
// database, so you know whether it's the same or not, and you still have encryption.
$select = "SELECT username, password FROM userinfo WHERE username = '$username' AND password = '$password';":
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) {
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
$_SESSION['controller'] = true;
// I use controller to control what they can view/not view when logged in, I remove the login panel and
// everything when there logged in, so I get full control with the controller session item.
}else {
echo "Go to hell you haven't logged in right";
}
}

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#35 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:45 PM

Now for cutting on and off people viewing things it's like

if ($_SESSION['controller'] === true) {
// show what you want to show
}else {
echo "You have to be logged in to view this";
}

or you can reverse it to NOT display something WHEN someone is logged in

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#36 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 02:47 PM

wo wo wo wo!!!!!!! people!!!!!!!


i specifically said when posting in this topic u gota keep things as simple as u can. Like ur talking to a person who just discovered comps even.

its what people do to confuse us noobs even more-shove a load of technical stuff in our mind and expect us to understand.

KEEP IT FRIGGING SIMPLE! I CANT EXPRESS HOW AGGRIVATING IT IS WHEN PEOPLE DON'T EXPLAIN SIMPLY! IT'S NOT JUST ME EITHER.

please can you explain that but simple for me please. because i dont even know any php command or what they do to do with sessions other than session start and session['summit'] = bla

please explain-simple

Pudgemeister

ps-Keep It Simple
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#37 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:50 PM

The bottom line is if you want it fed through a spoon go find another career.  I learnt in 3 months, if I would have learnt SIMPLY I wouldn't have learn for 4 years, you either want to learn it or you don't.  If you do shut up whining, take people's advice and get your ass out there and program.  Books will help< I know this, but understand, too many books will waste your time you ahve to get your ass out there and build applications yourself to be a programmer.  Simpler won't help.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#38 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 02:58 PM

and if you want sessions, sessions are simple.

On a basic level there are only 3 parts to sessions.

RULE NUMBER 1-
ALWAYS have session_start();
at the very top of the page, BEFORE ANYTHING ELSE
and don't keep any output, like echo's or print's too close to it, or it will still give you headers already sent.  That is pretty straightforward, and always have session_start(); on every page you are wanting the sessions to carry over too.

RULE NUMBER 2-
if you want to create a session it's simple
$_SESSION['username'] = $password;
That's it, the same way you create a variable, and if you can't create a variable then how the hell can you learn sessions.  Always it's the same thing.

RULE NUMBER 3-
when you begin getting ready to read those sessions(same as reading an array
$_SESSION['username']
that's it
or
$_SESSION[username]
if it's in extrapolation (THIS MEANS- putting a variable inside of the middle of text for instance

echo "Hello this {$variable} got extrapolated in this echo statement.

The same thing for sessions
echo "Hello this {$_SESSION[variable]} got extrapolated in this echo statement.";
if you don't understand the basics of what I am talking about now, stop doing sessions, and go back and pick up php for dummies, until you have next to mastered

1. variables and general variable theories
2. arrays
3. control structures examples if, elseif, while, do while, foreach, and whatever else.
4. basic to intermediate string manipulation

once you know those THEN you can probably safety attempt sessions.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#39 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 03:01 PM

ummm-thanx for simple (yet sort of rude) explanation o_0

people have listened to me in past posts in this topic and posted things simply without being rude.

but all the same-thanx to everyone. il go try it now.
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#40 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:04 PM

if ($X != 1) {
// do this
}

or

if ($X != 1) {
// do this
} else {
// do this if it is equal to 1!
} // end if/else statement
were do you get simple from, some of the stuff people posted on this, I don't even understand yet, some of this was far from simple.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users