Trying to 'understand' PHP

The basic concepts

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 2091 Views - Last Post: 22 August 2008 - 07:22 AM Rate Topic: -----

#1 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Trying to 'understand' PHP

Post icon  Posted 21 August 2008 - 02:51 AM

Hello,

I've been reading a few tutorials on how to learn to PHP but I think i'm failing to understand the whole concept of how it works. This is how i thought PHP would work, I was hoping people could clarify if I've got the idea right:

Let's say I have a website where i want most of the pages to have restricted access to members who can sign up.

- I must have a mysql database on the webhost, in there i create a table and put in fields like first name, surname, email etc. this is where member info is stored.

- I have a code on each restricted page that doesn't let anyone access them unless they're logged in. If they're not logged in they're redirected to a log in page

- The log in page has a basic form for the user to input their details, from what i've gathered from tutorials, the form has some kind of reference to another .php page, this has a bunch of coding that carries out the function of inputting the info to the database (i assume the registration page is the same concept, but makes a new entry in the database)

- The user is logged in with info that matches the database info, so they're allowed access to the restricted pages. The user is kept logged in until they log off - i have no idea how this works.

- Certain pages will 'echo' information, saying things like "Hello Mr John Smith, you currently have 5....etc"

Have i understood well or have i got it wrong? I don't want to rush into learning the language if i don't know the structure of it.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to 'understand' PHP

#2 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 03:03 AM

- I must have a mysql database on the webhost, in there i create a table and put in fields like first name, surname, email etc. this is where member info is stored.

Correct :)

- I have a code on each restricted page that doesn't let anyone access them unless they're logged in. If they're not logged in they're redirected to a log in page

Correct :)

- The log in page has a basic form for the user to input their details, from what i've gathered from tutorials, the form has some kind of reference to another .php page, this has a bunch of coding that carries out the function of inputting the info to the database (i assume the registration page is the same concept, but makes a new entry in the database)


Correct (the form points towards another using the action attribute - i.e.:)

<form method="post" action="gotothisfile.php">



The method="post" means that all the form data will be sent to the page specified in the action attribute, and can be gathered from that page like so:

$whatwasonlastpagesform = $_POST['mytextfield'];



- The user is logged in with info that matches the database info, so they're allowed access to the restricted pages. The user is kept logged in until they log off - i have no idea how this works.


Correct :) - When a match is found for the entered username and password, a $_SESSION is normally assigned (ensure 'session_start();' is at the top of any pages where you need to check if a user is logged in AND on the login page itself) - Check out some of our login tutorials in the tutorials > PHP section.

- Certain pages will 'echo' information, saying things like "Hello Mr John Smith, you currently have 5....etc"

Correct :) - you can display whatever information you want. For php to show something on screen it uses something like: echo 'Show this on screen';

So... looks like your on the ball :)

This post has been edited by pemcconnell: 21 August 2008 - 03:06 AM

Was This Post Helpful? 1
  • +
  • -

#3 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 03:16 AM

Wow thanks lol, that cleared a lot of doubt. There's one little thing i dont get tho, you mentioned this code:

$whatwasonlastpagesform = $_POST['mytextfield'];


When it comes to the dollar sign, i assume these are variables? what do i name them? does it even matter?

I found a form code from a tutorial and it goes like this:

<form action="action.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>


So would i then have:

$name = $_POST['name'];
$age = $_POST['age'];


Is this right?

This post has been edited by Sayid Ahmed: 21 August 2008 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 03:26 AM

That's right, but there are 'keywords' which are words reserved by PHP that you can't use. For example $this is used for classes. But yes, 99% of the time you can call a variable whatever you want. E.G.

<?php

$thisismyvariable = 'Hello';
echo $thisismyvariable;
?>



And for your form:

$name = $_POST['name'];
$age = $_POST['age'];



would be in your action.php file (you could have the form on the same page but best not to complicate things when you are starting off)

Put that form in a file called myform.php, which should look like this:

<html>
<head>
<title>My Form!</title>
</head>

<body>

<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

</body>
</html>



And that other code in a file called action.php, which should look like this:

<?php

echo 'You entered: '.$_POST['name'];
echo 'You entered:'.$_POST['age'];

?>



And see what happens.

You're picking everything up pretty quickly dude - If you have any questions don't hesitate to give me a shout :)

This post has been edited by pemcconnell: 21 August 2008 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 06:53 AM

Thanks again, i was looking at the PHP tutorial for a log in system here

I understood it all until this part:

require('conn.php'); // Holds all of our database connection information




I don't understand what i'm supposed to do with conn.php

This post has been edited by Sayid Ahmed: 21 August 2008 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 07:18 AM

at the minute I would assume that you have been coding on a 'page-per-page' basis. i.e. All the code you need for a page, will be in that page. Confused? Let me explain.

Say you have a PHP page called 'mypage.php'. The code for mypage.php is as follows:

<?php
$thisismyvariable = 'hello world!';
echo $thisismyvariable;
?>



Now, say you have to print $thisismyvariable on every page in your site. It would be easier if you could just set that variable in a page of its own, then call that PHP file everytime it is needed. You can use something called require(); to do this.

i.e.

Say you want to have a file where you want to put all your functions and variables that will be used multiple times across your site, in a file called 'functions.php'

functions.php would have the following code:

<?php
$thisismyvariable = 'hello world!';

function testFunction(){
	 $showthisonmyscreen = 'This is just a test function for this example';
	 return $showthisonmyscreen;
}
?>



Now, in your origional file, 'mypage.php', you can delete the line that said $thisismyvariable = 'hello world'; and replace it with require('functions.php');

i.e.

mypage.php

<?php
require('functions.php');
echo $thisismyvariable;

echo testFunction(); //i just threw a function in for example purposes
?>



This would perform the exact same thing as the origional (and echo the result of the added function).

Why bother?

Well, as you learn more and more about PHP, the one thing you might start to think is 'God, I wish I only needed to code that once', or 'Damn! I need to change that variable on every page!'. If you use the require() to load content, then you only need to add / edit / remove the code contained in that file once, which in turn updates every instance of that variable / function on your site, where it is called using the require.

To put it as easily as I can, require loads the specified file and copies all its code into the file and position that you are calling it from.

So...

the require('conn.php'); that tutorial is talking about means that there is a file called conn.php which has to be loaded into your script.

conn.php, connect.php, db.php dbconnect.php etc... are all common names people give the file that contains the database connection.

a conn.php would look something like this:

<?php
$servername='localhost';//replace with your servername
$dbusername='root';//replace with your username
$dbpassword='mymysqlpassword';//replace with your password
$dbname='thedatabasename';//replace value with your database name

$dblink = mysql_connect ("$servername","$dbusername","$dbpassword")or die('CONNECTION ERROR: Could not connect to MySQL');
mysql_select_db("$dbname",$dblink) or die ('could not open db'.mysql_error());
?>





Once you have mastered using the require(), you should look up include, and require_once(). You'll find that require_once() will be used more often when loading functions and constants.

This post has been edited by pemcconnell: 21 August 2008 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 07:22 AM

That makes a lot of sense, yes i understood the 'page-per-page' before, it's just the tutorial didn't explain what to do with conn.php, but i get it now, every time i wish to input information to the database i use require('conn.php'); - would this apply to member registration too?

thanks again pemcconell

This post has been edited by Sayid Ahmed: 21 August 2008 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#8 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 07:30 AM

Any page that needs to communicate to the database would need that require('conn.php'); in it. I generally include my connection file in all my pages, (most websites and games i make, need it on every page).

Good luck man :)

This post has been edited by pemcconnell: 21 August 2008 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

#9 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 07:39 AM

So most of the pages have the code at the top:

session_start();
require('conn.php');


How do i go about redirecting the users who haven't logged in? a code that says if the session fails send them to this page?


EDIT: i made the conn.php page but i get a mysql connection error

This post has been edited by Sayid Ahmed: 21 August 2008 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 07:55 AM

have you changed the values to suit your database?

i.e. These lines should be customised:

$servername='localhost';//replace with your servername
$dbusername='root';//replace with your username
$dbpassword='mymysqlpassword';//replace with your password
$dbname='thedatabasename';//replace value with your database name



You need to enter in your server name (localhost if your working off your own PC), your username (probibly root if you're working off your own PC), your password (not set initally by the likes of xampp, so either set this on your server (go to security if you are using something like xampp) or set it as blank '') and finally your database name.

So, say your details were as follows:

username: root
password: football
servername: localhost
database name: logindb

You code would look like this:

$servername='localhost';
$dbusername='root';
$dbpassword='football';
$dbname=' logindb';



Let me know how you get on, and I'll show you how to do the login part.

This post has been edited by pemcconnell: 21 August 2008 - 07:57 AM

Was This Post Helpful? 0
  • +
  • -

#11 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 08:05 AM

yeh it seems my dbname was wrong, i tried to log in and it redirected me to a failed log in page, so far so good.

Now how do i go about signing a user up? or at least for now, editing my database so i can put my own users.
Was This Post Helpful? 0
  • +
  • -

#12 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 08:13 AM

you'll want to set your database up first.

Create a table, call it whatever you like (e.g. tblusers)

In that table I would create 3 columns:

userId - Primary Key, Auto-Increment
userName - varchar(20)
userPwd - varchar(32)

Once you have done that let me know
Was This Post Helpful? 0
  • +
  • -

#13 Sayid Ahmed  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 156
  • Joined: 20-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 08:23 AM

i'm getting "#1063 - Incorrect column specifier for column 'id' " when i select auto increment
Was This Post Helpful? 0
  • +
  • -

#14 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 08:35 AM

you might need to google that one - I'm not familliar with it.

However,

I made you the SQL code to make your table:

(If you are using phpmysql, go to the SQL tab and enter this)

CREATE TABLE `dbtest`.`tblusers` (
`userId` INT NOT NULL AUTO_INCREMENT ,
`userName` VARCHAR( 32 ) NOT NULL ,
`userPwd` VARCHAR( 32 ) NOT NULL ,
PRIMARY KEY ( `userId` ) 
) ENGINE = MYISAM 



The only thing you need to change is the first line. Change dbtest to your database name. (You'll need to delete tblusers if you already have a table called that, before you run this SQL statement)
Was This Post Helpful? 0
  • +
  • -

#15 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Trying to 'understand' PHP

Posted 21 August 2008 - 08:39 AM

Hey I wanted to send a quick apology. I wrote that tutorial you're using, and a few issues have come up. First, make sure you're not using stripslashes(). That was stupid of me. Use mysql_real_escape_string() instead.

I also clarified the conn.php issue in the comments section of the tutorial. Thanks for the inspiration.

Incorrect column specifier for column 'id' means that you have your 'id' column set to the wrong type. You probably have it set to VARCHAR. Change it to INT and it should work.

As far as telling if a person is logged in or not, you will want to use session variables. At the top of every page you want to use a session variable in, put the code

session_start();



You then set session variables like so

$_SESSION['loggedIn'] = "true";



After a successful login, you'll want to use the above code. That will create a session variabled called loggedIn, and set it to true.

After doing that, any page that you want to have restricted should put the following code at the top

if ($_SESSION['loggedIn'] != "true")
{
     header("Location: login.php");  // Set to whatever your login page is
}



There are other ways to do authentication, but this is the most basic.

Let us know if you have more questions, and sorry if the tutorial was unclear.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3