Jump to content

Archived

This topic is now archived and is closed to further replies.

Pudgemeister

Noobie Stuff: Very Basics Explained Easily

Recommended Posts

[code]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';
};
?>[/code]
why do I see a session start popped in the middle of all that code??

Share this post


Link to post
Share on other sites
also you don't call a session variable like that it's
$_SESSION['whatever'] = "whatever";
you have your session in all lowercase.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';[/code]
also you should give your coding better form.
[code]
echo "Your Password And/Or Username Are Not Correct-Please Try Again <a href=\"index.php\">Here</a>";[/code] 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
session_start();
at the top of EVERY page used with sessions.

Inside the php.ini
[quote]Session]
session.save_handler[/quote]
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.

Share this post


Link to post
Share on other sites
example
You have a register form
where they sign up
don't forget on the password to put

[code]$passhash = md5($password);[/code]
or something similar
Then when your ready to test, do this.
[code]$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";
}
}[/code]

Share this post


Link to post
Share on other sites
Now for cutting on and off people viewing things it's like

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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]if ($X != 1) {
// do this
}

or

if ($X != 1) {
// do this
} else {
// do this if it is equal to 1!
} // end if/else statement[/code]
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.

Share this post


Link to post
Share on other sites
and I apologize for being rude, stuck on a dumb ass under-priced huge project, and I am a little pissed.

Share this post


Link to post
Share on other sites
nah im sory as wel-i startin college in a month and am lookin foreward to it so much i sorta tryin to get head start.

i am learning php for a reason though-i have planned-my own big project that wil take a while-ts why im learning php in the first place.

do u understand that code in ur comment?

i remember it from when i couldnt even do if else codes hehe-think thats why he posted-to explain.

if u dont i wil explain but i am guessing u do hehehe.

ok i got my own prob now but i think i can sort it (i think)

even when i put in the right username and password, now it still says somethin sis wrong :S

oh well itl b ok.

cheers
Pudgemeister

ps sorry again

Share this post


Link to post
Share on other sites
yes at this point, I understand almost everything I see with php/mysql, because of the hardships I faced in recent projects, I feel as thought I can do almost anything with php.  My current difficulty is pagination but I am not doing that right now, I will create a new system myself later on down the road when I have time.  The reason I learnt so fast, is I started creating everything I did from scratch.

Share this post


Link to post
Share on other sites
its not an error-its the fact i been trying to get something but doing the wrong thing to get it.

like i said erlier-in the table "users" in the database there is a field called "id" which is auto_increment.

well i expected the

[code]$query_2="SELECT id FROM users WHERE username = '$username' && password = '$password'";
$result_2=mysql_query($query_2) or die(mysql_error());
$id=mysql_num_rows($result_2);[/code]

to get the id for that user that has successfully logged in correctly.

of course that is totally wrong as all the $id variable is going to be is the number of rows that have the username and password to be what the user typed in (which since its a log in script, will always be 1 as no 2 usernames can be the same.

i am trying to grab the id from the database that corresponds to the username and password and have it assigned to the variable "$id".

corresponds? big word for me hehehe :S

lol

Pudgemeister

Share this post


Link to post
Share on other sites
when you are allowing them to sign up simple have
[code]<?php
$select = "SELECT username FROM userinfo WHERE username = '$username';";
$query = mysql_query($select);
if (mysql_fetch_array($query)) {
echo "I apologize but the username is already taken";
}
?>[/code]
that's it,
as for pulling the info where user name and password is tested

after they click submit to login it's
[code]<?php
$select = "SELECT username, password FROM userinfo WHERE username = '$username' AND password = '$password';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) {
echo "You have logged in successfully";
}
?>[/code]

these examples won't work but it'll give you an idea on wher eto start.

Share this post


Link to post
Share on other sites
oki could understand all of that but me being me i still made a bolocks up of my code.

[code]if ($num===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' && password = '$password'";
$result_2=mysql_query($query_2) or die(mysql_error());
$id=mysql_fetch_array($result_2); // returns the data found-hopefully.
print $id;[/code]

all i need now is:

to assign the id number found in the table in the database to the variable $id and then print it.

but no...me being me-i cant do it.

Pudgemeister

Share this post


Link to post
Share on other sites
[code]<?php
if ($num===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' && password = '$password'";
$result_2=mysql_query($query_2) or die(mysql_error());
$id=mysql_fetch_array($result_2); // returns the data found-hopefully.
print $id;
?>[/code]
ok if you don't clean this up like I said you'll regret it later your echo.
And your trying to access your id wrongly.  Here is the thing you don't understand right now
mysql_fetch_array()
is meant to capture an ARRAY into the variable, meaning you have to understand arrays to do this, that's whY i said earlier basic's first, you can't jump straight to mysql queries without knowing other stuff, like arrays and general variables.

THis is what your code should look like
print is fine, but I find echo better, if you use print 1 itme in your script use all print, if you use echo, then use all echo it's neater.

here is your code again, with what it should look like if you want it to do what you want.
and I cleaned it up a bit.

[code]
<?php
if ($num===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' AND password = '$password';";
$result_2 = mysql_query($query_2)
$id = mysql_fetch_array($result_2); // returns the data found-hopefully.
echo $row['id'];
?>
[/code]
as you can see a few thigns you were doing are bad habits.  Now the $row calls the array if you had of use
$shithead = mysql_fetch_array($result_2);
then you access it
$shithead['id'];
same thing
you access the array, followed by the value
$row['id'];
if you had other stuff you just replace the variable with the field name in your database
$row['variable']:

Share this post


Link to post
Share on other sites
You also have to realize that MySQL syntax is different than PHP syntax.  PHP uses comparison operators (such as == and &&), MySQL doesn't.  You had these PHP operators in your MySQL query which was causing your errors.  I know you want to learn everything as simple as posible, and avoid all of the technical jargon and whatnot, but you're going to kick yourself later when you HAVE to go back and relearn everything as you should have learnt it the first way.  Believe me, it's not that hard if you just take the time to work with it.  Like in the previous post, you are going to have to understand both MySQL and arrays before doing yoru database queries.  You can't just echo your array without accessing an individual element.  If what I'm saying confuses you, do a quick google search for PHP arrays and read some articles.  This is very basic stuff and what you are trying to accomplish cannot be done without understanding it.

Good luck.

Share this post


Link to post
Share on other sites
yes when I rewrote the code I took that out, you can use some comparison operators but on variables, like less than greater than sometimes using sql operators are more helpful than pulling them into php and doing it, but for statements you HAve to use
[quote]AND
OR[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.