LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using PHP & MySQL to make a small web puzzle; help? (https://www.linuxquestions.org/questions/programming-9/using-php-and-mysql-to-make-a-small-web-puzzle%3B-help-562139/)

Jorophose 06-15-2007 07:31 PM

Redirection in PHP after IF / ELSEIF / ELSE statements
 
Scroll down to later in the topic.

FMC 06-16-2007 12:18 PM

I will try to tell the basic things that you will need.

First of all: php.net is great!

To identify the user you can start a session with
Code:

http://php.net/session_start
session_start();

Than you can register an array to identify the user and keep some informations about this person.

Code:

http://php.net/session_register
session_register();

You can put this code on an include file and include it on every page. Sure, just register the session when the user type the correct username/password.

To check the user name you can read the database with mysql_* functions, create an include file with your connection informations to make it easy:

Code:

http://php.net/mysql_connect
mysql_connect("server", "username", "password");

Don't forget to select a database:
Code:

http://php.net/mysql_select_db
mysql_connect("your_database");

Now you already have a open link and you can use some functions to check things and etcetera:
Code:

http://php.net/mysql_fetch_array

if($user = mysql_fetch_array(mysql_query("SELECT id, group FROM user WHERE name = '$_POST[username]' AND password = '$_POST[password]' AND active = 1"))):
  session_register("$UserInfo");
  $_SESSION[UserInfo][id] = $user[id];
  $_SESSION[UserInfo][group] = $user[group];
else:
  echo "<form to input login information>";
endif;

You can also use mysql_result to get a specific result:
Code:

http://php.net/mysql_result
$full_name = mysql_result(mysql_query("SELECT full_name FROM user WHERE id = $UserInfo[id]"),0,0);

To insert values you can just run mysql_query:
Code:

http://php.net/mysql_query
$query = "INSERT INTO user (name, password, fullname) VALUES ('foo', 'bar', 'Foo Bar Baz');
if(!@mysql_query($query)):
    echo "Error inserting values";
endif;

If you need a lot of results you can run mysql_fetch_array in a fashion way:
Code:

http://php.net/mysql_fetch_array
$query = mysql_query("SELECT id, user, fullname FROM user WHERE active = 1");
while($res = mysql_fetch_array($query)):
  echo "$res[id] - $res[user] - $res[fullname]<br>";
endwhile;

Be careful with mysql_injections, to avoid this kind of problem you will have to read about mysql_escape_string and mysql_real_escape_string on php.net:
http://php.net/mysql_escape_string
http://php.net/mysql_real_escape_string

Well, I believe that you have some material to start building your app, go for it... and if you have some doubt just ask!

[]'s, FMC!

Jorophose 06-16-2007 03:41 PM

Thanks FMC for the tips. I had used the steps here (http://www.php-mysql-tutorial.com/us...n/database.php) but they don't seem to be working well.

Regarding the code you gave me, does it all go inside the same PHP file? (Except for the one that is used with php include().) Do I add the usernames & passwords myself? Do I need to include the php.net links, or are they just for refference?

And is there any way to keep track of stuff a user has picked up/achieved?

For inserting usernames & passwords, can I do that manually in a database? Or is there a SQL script/program/etc. I can use to do it for me?

Thanks!

FMC 06-16-2007 05:38 PM

Well, I just gave you some tips, you will have to use your creativity to put everything together and build your app!

The links to php.net are just for reference, there you can know everything the function does (or do, I don't know, I'm braziliam)!

The first important thing to start to code is to be sure what your software will be, you have to imagine how it will work and everything, try to imagine something like:
1 - A form to input log/pass.
2 - If the log/pass is correct, than register the session and redirect the user to the initial page, it it is incorrect show the form again.
3 - Show something to the user and let him decide what to do.
4 ...

I don't know exactly what you want to do, even if I knew it would be impossible to me to give you the exact lines that you need, that's why I'm linking everything to you, this way you can study and do it by yourself!

You could start by doing something like:
connection.inc.php (a include for every single page)
login.inc.php (to be sure that the user is logged in, if not, redirect to log page)
login.php (form for the user to login and register the session)
index.php (page to show something for the user after login)

Oh, another function that you will probably like is "header", with this function you can redirect the user to another page: php.net/header (search for "Location").

[]'s, FMC!

Jorophose 06-16-2007 05:46 PM

Ah, thanks for the clarification.

I should be good to go now, so I guess have to test it out and see what happens now. :)

Cheers,
Joro.

chrism01 06-17-2007 12:27 AM

Just in case you don't have it, here's the online refs to all the MySQL docs, inc examples, for all version from 3 -> 5; see top left section of page for the link to your version.
http://dev.mysql.com/doc/refman/5.0/en/

Jorophose 06-17-2007 06:15 PM

Thanks chrism, I'll have to check that link out later though, right now I've got an impeding problem.

http://slycorps.6te.net/quest/
If you go there, and type in the word bar, you get a page that tells you "you're in a bar" but if you type in phonebooth, it tells you "you're in a phonebooth" and then gives you the error message right after.

Code:

<html>
<head>
<title>POST and GET functions</title>
</head>
<body>

<?php
$destination = $_POST["destination"];  /* This will get the name variable input from the form above */
$booth = 'phonebooth';        /* This should make the term booth in the box mean booth here */
$bar = 'bar';            /* See above, but the bar */

if ($destination == $booth){
  echo "You are in a telephone booth.";
}

if ($destination == $bar){
  echo "You are in a bar.";
}

else { echo "Sorry, I don't understand what you meant by $destination :("; }

?>

</body>
</html>

Here's my code, I'm hoping someone can help me spot the problem. This is possibly my first serious shot at programming something, and I had to work some issues out myself. Also, is there any way to redirect a user to another page rather than use "echo"?

FMC 06-17-2007 06:32 PM

As I already told, you can use the header function to redirect the user to another page:

header("Location /yourpage.php");

[]'s, FMC!

Jorophose 06-17-2007 06:45 PM

EDIT: This is getting crazy. Turns out closing the brackets after each echo, and making repetitive statements "elseif" works.

But how do I do header functions?

matthewhardwick 06-19-2007 04:52 AM

I know I may be smacked in the head for saying this, but Dreamweaver can do all this and more. You can build a comprehensive system from the ground up very quickly and easily, and it will look after SQL and Users etc. for you.

gamma9mu 06-19-2007 12:19 PM

Quote:

Originally Posted by matthewhardwick
I know I may be smacked in the head for saying this, but Dreamweaver can do all this and more. You can build a comprehensive system from the ground up very quickly and easily, and it will look after SQL and Users etc. for you.

will dreamweaver teach you PHP and MySQL?

Jorophose 06-19-2007 05:04 PM

It probably won't. And on top of that, I don't have 400$ (Or whatever amount it is now) to spend on Dreamweaver, I don't think I can run it well, and school's out so I can't use the labs.

I still haven't figured out how to use "header" after an if/elseif/else statement. Any ideas? And can I send a user back to a page + echo something new?

chrism01 06-20-2007 06:14 AM

Also, afaik, Dreamweaver has never been ported to Linux ... unless you're using virtual OS.
Anyway, you won't learn as much that way.

Jorophose 06-20-2007 06:25 PM

Plus it doesn't seem to be working under Wine right now, and I doubt CrossOver has a solution...

Man, if only Adobe would bring the creative suite to Linux. I mean, they can keep it closed if they want... But native Photoshop on Linux is a huge boon.

[/offtopic]

Guys, I could use a bit of help here!

I can't make that bloody "header" function do anything when it's in an IF statement! Any fixes? :(

FMC 06-20-2007 06:50 PM

Do you receive any warn message when you execute your if statement?

The header function will not work if there is some output to browser before it is called. A simple \n is sufficient to make header not work at all!

Try this code and take it as a example:
Code:

<?php
if($_GET[test] == 1):
    header("Location: http://www.google.com");
elseif($_GET[test] == 2):
    header("Location: http://www.yahoo.com");
else:
    echo "<a href=\"$PHP_SELF?test=1\">click here to go to google</a><br>
          <a href=\"$PHP_SELF?test=2\">click here to go to yahoo </a><br>
        ";
endif;
?>

[]'s, FMC!


All times are GMT -5. The time now is 03:59 PM.