ASE Labs
Welcome Guest. Please register or log in now. There are 1618 people online (0 Friends).
  • Home
  • Articles
  • News
  • Forum
  • Register/Login

PHP Question

There are 15 messages in this topic Watch this topic for replies Subscribe to this topic
Add Reply Back to forum "Software Discussion" Back to Index
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
PHP Question Old Mon Oct 1, 2007 10:31:10 AM #41762 Perm Link
Hello everyone,

I'm wondering how to make a page where people can register. I know the seperate basics, I know for example:

- how to make a form that upon submitting creates new entries in a tabel (using INSERT INTO)
- how to make a contact form that emails information to an e-mail adress provided
- how to validate the contact form

What I want to be able to do is this, I want a form with the fields: username, password and e-mail adress. When pressing the submit button I want this to happen:

- the form to be validated (including true e-mail adress validation)
(- if validation fails, echo the error messages at the fields where validation failed (example: "You did not provide a valid e-mail adress"))
- the information entered must be sent to the e-mail adress provided so that a visitor has their password saved
- when done, the script should redirect the visitor to a new page within the site, LOGGED IN

now what I don't know is, how do I (roughly) order everything? As in: in what order should I write the PHP parts or should I spread these actions over several scripts? Also I'm not the greatest with all the if / else tags. How do I make sure that everything goes the way I want it? Anything would help, being it a complete script (easiest Razz ) or a quick sketch in paint. So this is what I need:

CHECK if form is filled in correctly
IF it is not filled correctly, echo errors
[i](this just came to my mind, I also want to make sure that the user came from my site so that it is not a spambot, using:

Code

<?php
$previous_url = getenv("HTTP_REFERER");
if ($previous_url != "http://www.example.com/register.html") {
echo "You can't call this script from a different location";
exit;
}
?>


IF filled correctly, e-mail information to e-mail adress provided (using $POST[visitormail] for example)
REDIRECT to new page
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Mon Oct 1, 2007 10:47:32 AM #41764 Perm Link
This is for a class right? Unluckily for you, I won't give you the answers Smile, I will give you bits and pieces to get you on the right path. Let me remind you if you didn't know or forgot: http://www.php.net is the prime resource for functions and such.

Let's break the problem into parts.

True email validation: This is not that difficult. You can only really test the DNS records of the domain name to see if there is a MX record. Use this function: http://us2.php.net/manual/en/function.checkdnsrr.php You will need to strip out the domain name off the email by using a regex, but that is pretty simple.

Sending Email: http://us2.php.net/manual/en/ref.mail.php

Logging in: That's a job for: http://us2.php.net/manual/en/function.setcookie.php

Now, logistically, all these functions should be in a separate function file and you would just call the function...

Code

//functions.php
<?PHP

function checkemail($email)
{
     //this function will check the dns records of the email address
     //It will return true or false
     /*


          Bla bla bla


     */

     return 1;
}
?>

Code

<?PHP
//Calling page...
include('functions.php'); //Grab the functions


//Got email address from form.
//Does it have extra slashes in it?
$email=stripslashes($_POST['email']);

if(!checkemail($email))
{
     exit('Email address is invalid');
}

?>


You can then reuse these functions anywhere you want.

The last bit about the referrer is a bit strange when put into practice. You're better off displaying a simple math problem and having the user answer it... Like what is 2+2? That will defeat the bots. You can also use session and tons of other stuff.

I know I'm vague. If you need actual code samples, just ask and I will show them.

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Mon Oct 1, 2007 1:00:54 PM #41767 Perm Link
Hey Aron, thanks a bunch and I'm sorry for my late reaction. I've been busy designing a flyer over the last few hours - so many things on my mind!
I'll take an indepth look to your answer and the links you provided now, I'll let you know once I've finished or if I'm still having trouble Smile .

The idea of comparing the referrer site and the correct referrer was in my PHP book, I think it's good enough for the assignment in class Smile .
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Mon Oct 1, 2007 1:02:07 PM #41768 Perm Link
In response to tomhaagmans #41767
Yeah, don't go too overboard Smile. I did and I created the first version of ASE.

You're very lucky to have that type of class. I'm sure you'll do fine in it.

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Mon Oct 1, 2007 3:35:43 PM #41770 Perm Link
Yeah it's an awesome study too, I get marketing, communication, audio/video, graphics design and ICT.

I've only just managed to call an external function, I was testing it with making a simple table:

Function.php

Code

<?php
function kader($tekst) {
echo "<table border=\"1\">\n";
echo "<tr><td>".$tekst."</td></tr>\n";
echo "</table>\n";
}
?>


Index.php

Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
include "function.php";
kader("TEST");
?>
</body>
</html>


I'm progressing really slow due to a lot of other work AND personal issues also Sad . Once more, thanks for your time though! Also on a side note, would you mind closing Photoshop This! on agfx and making a new one? I'm really sorry but I'm not sure if I have the time searching for a new picture.
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Mon Oct 1, 2007 3:41:47 PM #41771 Perm Link
I'll whip up a good example later tonight... This is a place holder post.

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Tue Oct 2, 2007 2:24:49 AM #41811 Perm Link
I've also stumbled upon a new problem. My teacher tried explaining this to me but it still seems pretty abstract to me.
What I'm basically making is a time scheme for a anniversary festival. The acts playing on the festival will have different genres such as music, dance, theatre etc.
When drawing the table, using for example SELECT * FROM acts WHERE day = thursday, I want every table row (<tr>Wink to have a CSS class corresponding with the genre of that act. So that the rows for all music acts would have for example a green background and white text, whereas the dance rows could have a red background and black text.

If you could help me by giving the code for two different genres with very minimalistic CSS (for example only background: #COLOR) I'm sure I could do the rest Smile .
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Tue Oct 2, 2007 4:54:00 AM #41815 Perm Link
In response to tomhaagmans #41811
That one is very simple... We'll just do inline css:

Code

<table>
     <tr style="background:#00f; color:#fff;"><!-- Start of row: Background is totally blue and text color is white-->
          <td>bla bla bla</td>
     </tr>
     <tr style="background:#f00; color:#0ff;"><!-- Start of row: Red background, text is tealish. -->
          <td>Bla bla bla bla</td>
     </tr>
</table>


That's an example of inline styles. You can just paste that code in an html page to see how it renders.

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Tue Oct 2, 2007 5:09:13 AM #41823 Perm Link
Heh I know that much mate Smile . But for that to work I'd have to know exactly what time plan entries I have.
Now imagine the administrator submitting new acts in the backoffice!

I've managed to make it so that it'll display everything in the correct order (SORT BY time ASC) but I have to make sure that new entries submitted get the right CSS to go along with them (corresponding with the genre) .

Hope you know what I mean Smile .
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Tue Oct 2, 2007 6:17:44 AM #41829 Perm Link
What does the table structure look like? You can do

Code

<table>
<?PHP while($row1=mysql_fetch_array($q))
{
     switch($row1['genre']) //Pick colors based on genre
     {
          case 'music':
          $bg='00f';
          $fg='fff';
          break;
          case 'dance':
          $bg='f00';
          $fg='0ff';
          break;
     }     
     ?>
     <tr style="background:#<?PHP echo($bg); ?>; color:#<?PHP echo($fg); ?>;"><!-- Start of row echo in colors-->
          <td><?PHP echo(htmlentities($row1['data'])); ?></td>
     </tr>
     <?PHP
}   
?>

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Wed Oct 3, 2007 2:22:08 PM #41974 Perm Link
The table structure will be quite simple, not 100% sure but it'll roughly be like:

Time________Act name______Genre______Location
===================================
00:00_______name__________rock_______location1
00<!--s51-->:3<!--/s51-->0_______name__________dance______location2

And every row will have to have a CSS style matching the genre.
The classmate I'm doing this exercise with came up with this idea:

Code

<?php
echo {
"<table>
<tr class=\"".$genre.">
<td>blahblah</td>
<td>blahblah</td>
</tr>
</table>"
}
?>


I'm not sure if his code is 100% right but the idea seems simpler then yours, right? Smile . I'm going to give his idea a try now, see if I get it to work.

Main problem is that we both are suckers with SQL, I wonder how to make it that $genre is loaded from the database properly, as well as the content for every td.
And when I've got round to doing it, I'll have to put it in an array as well.
And then make it sure that it's sorted by time O.o

Damn this is going to be a shitload of work XD .
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Wed Oct 3, 2007 2:55:38 PM #41984 Perm Link
In response to tomhaagmans #41974
My code and your do exactly the same thing.

You have your table schema (SQL), then just fetch the array and you're good to go.

What does your table schema look like?

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Wed Oct 3, 2007 3:25:52 PM #42006 Perm Link
What exactly do you mean for the schema? The layout is like I've posted above Smile .

Do you have any idea how to fetch the information from the database using MySQL?
Or how to put this idea to work in an array?

I'm so bad with PHP, darn! And thing is, my classmate isn't good either so we're in a baaaad group actually Huge Grin .
Reply Quote Multi-Quote-Off Send PM Profile
Aron Schatz Avatar 2014: Year of change. Joined: August 3, 2001 Status: Offline Posts: 10753 Rep: PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 3 (332767)PIP Level 1 (332767)
(No Title) Old Wed Oct 3, 2007 6:52:29 PM #42039 Perm Link
In response to tomhaagmans #42006
Relevant functions:
http://us2.php.net/manual/en/function.mysql-connect.php
http://us2.php.net/manual/en/function.mysql-select-db.php
http://us2.php.net/manual/en/function.mysql-query.php
http://us2.php.net/manual/en/function.mysql-fetch-assoc.php

In that order as well.

Example. To fetch data from your table with database name 'test' and username 'user' with password 'pass' from table 'data'...

Code

<?PHP
if(!mysql_connect ( 'localhost','user','pass')) //Connect to database or die
{
     die('There was a problem connecting to the database';
}
if (!mysql_select_db('test'))
{
     echo "Unable to select test: " . mysql_error();
     exit;
}

$sql ='SELECT * FROM data';

$result = mysql_query($sql); //Send query

if (!$result) //Bad query
{
     echo "Could not successfully run query ($sql) from DB: " . mysql_error();
     exit;
}

if (mysql_num_rows($result) == 0) //No results
{
     echo "No rows found, nothing to print so am exiting";
     exit;
}

while ($row = mysql_fetch_assoc($result))
{
     print_r($row);
}

mysql_free_result($result); //Release mysql result
mysql_close();
?>

2014 is going to be a good year. More content, more streamlining. Be a part of history!
Reply Quote Multi-Quote-Off Send PM Profile
tomhaagmans Joined: October 1, 2007 Status: Offline Posts: 10 Rep: PIP Level 1 (116)PIP Level 1 (116)PIP Level 1 (116)
(No Title) Old Thu Oct 4, 2007 3:28:24 AM #42054 Perm Link
Ah thanks mate, these functions have helped me! I've now managed to leech stuff out of the database and display it on screen, now I'm going to try changing the layout with CSS depending on the output data.
Reply Quote Multi-Quote-Off Send PM Profile
Page: [1]
Add Reply Back to forum "Software Discussion" Back to Index

Quick Reply

Login
Welcome Guest. Please register or log in now.
Forgot your password?
Navigation
  • Home
  • Articles
  • News
  • Register/Login
  • Shopping
  • ASE Forums
  • Anime Threads
  • HardwareLogic
  • ASE Adnet
Latest News
  • Kingston HyperX Cloud 2 Pro Gaming Headset Unboxing
  • Synology DS415+ Unboxing
  • D-Link DCS-5020L Wireless IP Pan/Tilt IP Camera
  • Actiontec WiFi Powerline Network Extender Kit Unboxing
  • Durovis Dive Unboxing
  • Bass Egg Verb Unboxing
  • Welcome to the new server
  • Gmail Gets Optional Preview Pane
  • HBO Go on Consoles
  • HP Touchpad Update
Latest Articles
  • D-Link Exo AC2600 Smart Mesh Wi-Fi Router DIR-2660-US
  • HyperX Double Shot PBT Keys
  • Avantree ANC032 Wireless Active Noise Cancelling Headphones
  • ScharkSpark Beginner Drones
  • HyperX Alloy FPS RGB Mechanical Gaming Keyboard
  • D-Link DCS-8300LH Full HD 2-Way Audio Camera
  • Contour Unimouse Wireless Ergonomic Mouse
  • HyperX Cloud Alpha Pro Gaming Headset
  • Linksys Wemo Smart Home Suite
  • Fully Jarvis Adjustable Standing Desk
Latest Topics
  • Hello
  • Welcome to the new server at ASE Labs
  • Evercool Royal NP-901 Notebook Cooler at ASE Labs
  • HyperX Double Shot PBT Keys at ASE Labs
  • Avantree ANC032 Wireless Active Noise Cancelling Headphones at ASE Labs
  • ScharkSpark Beginner Drones at ASE Labs
  • HyperX Alloy FPS RGB Mechanical Gaming Keyboard at ASE Labs
  • D-Link DCS-8300LH Full HD 2-Way Audio Camera at ASE Labs
  • Kingston SDX10V/128GB SDXC Memory at ASE Labs
  • What are you listening to now?
  • Antec Six Hundred v2 Gaming Case at HardwareLogic
  • Sans Digital TR5UTP 5-Bay RAID Tower at HardwareLogic
  • Crucial Ballistix Smart Tracer 6GB PC3-12800 BL3KIT25664ST1608OB at HardwareLogic
  • Cooler Master Storm Enforcer Mid-Tower Gaming Case at HardwareLogic
  • Arctic M571-L Gaming Laser Mouse at ASE Labs
  • Contour Unimouse Wireless Ergonomic Mouse at ASE Labs
Press Release
  • Huntkey Has Launched Its New Power Strips with USB Chargers on Amazon US
  • Inspur Releases TensorFlow-Supported FPGA Compute Acceleration Engine TF2
  • Hot Pepper Introduces Spicy New Smartphones in US Markets
  • Sharp Introduces New Desktop Printers For The Advanced Office
  • DJI Introduces Mavic 2 Pro And Mavic 2 Zoom: A New Era For Camera Drones
  • DJI Introduces Mavic 2 Pro And Mavic 2 Zoom: A New Era For Camera Drones
  • Fujifilm launches "instax SQUARE SQ6 Taylor Swift Edition", designed by instax global partner Taylor Swift
  • Huawei nova 3 With Best-in-class AI Capabilities Goes on Sale Today
  • Rand McNally Introduces Its Most Advanced Dashboard Camera
  • =?UTF-8?Q?My_Size_to_Showcase_Its_MySizeId=E2=84=A2_Mobil?= =?UTF-8?Q?e_Measurement_Technology_at_CurvyCon_NYC?=
Home - ASE Publishing - About Us
© 2010 Aron Schatz (ASE Publishing) [Queries: 19 (9 Cached)] [Rows: 377 Fetched: 72] [Page Generation time: 0.89714789390564]