Using PHP & MySQL to make a small web puzzle; help?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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;
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?
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").
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/
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"?
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.
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.
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?
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?
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;
?>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.