[A Really Easy Php Question] - Computers



Search Tech-Forums - link takes you to our Forum's search page.

Note: The following is only a text archive!


To view the actual forum discussion, please visit our website at http://www.tech-forums.net

Pages:1



A Really Easy Php Question

(Click here to view the original thread with full colors/images)



Posted by: tommyboy123x

Below is the setup/story, but the question is how do I put in a php variable as the value of a form input?

For the life of me I can't figure out how to put in a hidden form with a PHP value. here is what i want to have work

[php]
<input type="hidden" value="<?php echo $invite; ?>" name="invite">
[/php]

which is supposed to hold the value of how many invite codes the user has.

Only problem is, when i go to check the source (and attempt to echo the value), it says "Resource id #5"

So how can i put in $invite as the value?



Posted by: Babbage

Well, usually when PHP echo's out something like Resource id #5, it usually means that your variable is some form of handle, instead of a simple scalar variable.

Does invite hold a handle to a file or something?



Posted by: tommyboy123x

well $invite will usually have a value of 10, which is found by accessing the mysql database. At first i was estatic to find that the variables were hidden in that way because one of my other problems was that if a user changed it they would have more invite codes, but i didn't think it would actually echo Resoruce id #5.

Not sure if it matters, but the method that i am sending the data is POST. This is part of a login page - partially to keep robots out (not googlebot or anything, but "competition" robots), and partially to make things more simple on myself. You put in your info, if it is accepted, you go to a page that has a submit button where you click there, and it POST's the variables, such as $invite.

I'm not completelly sure what a handle is (temp name of a file..?), but it should just be a simple integer variable.



Posted by: Babbage

Can you post your code leading up to the use of this variable? It will be much easier to diagnose if you can do that (obviously you can leave out any sensitive information).


[QUOTE]I'm not completelly sure what a handle is (temp name of a file..?), but it should just be a simple integer variable.[/QUOTE]

A handle is basically the id given to connect to a specified file. In this case instead of a file, it is given to a database resource.



Posted by: tommyboy123x

Before i post this, the things it stores such as "shared" and "downloaded" is NOT a warez type of site... I'm not that stupid

i know its not the most optimized, but im still working on that part :)

[code]
The problem has been solved and the code has been deleted because i'd rather not post my code all over the place....
[/code]

So thats all of it, hardly what you may call "optimized", but it works for testing pretty well :)

Anyways, why can't it echo the $_COOKIE['invitekeys']?



Posted by: Babbage

[PHP]$invitequery = "SELECT invitekeys FROM hsmemberdb WHERE username = '$username' and ******** = '$password'";
$invite = mysql_query($invitequery);[/PHP]

This code is what is giving you the problem. See, when you call mysql_query(), it actually returns a result set, not the result. Its kind of like an array, that is holding your results (even if its only one result).

So, to get the information from your result set that you want, you will want to use one of the functions that php provides for accessing the array of the result set. I have included a link to one such function:

[url]http://us2.php.net/manual/en/function.mysql-fetch-assoc.php[/url]

Just take a look at example one. At the bottom is a while loop that goes through the result set, and grabs the information.

If you have any problems, just let me know.



Posted by: tommyboy123x

thank you!!

That worked perfectly!



Posted by: Babbage

Glad it worked for you. Good luck with your website!



Posted by: tommyboy123x

alright... this will definatelly be the last issue cause its the last thing i have to sort out... but basically, i can't get authentification to work right or i have some kind of minor error wrong :confused:

[php]
updated code below
[/php]

The way i put this in my code was using the include(), but i need it to somehow disallow access to those without the cookie stored.



Posted by: Babbage

Well, if the person has the cookies on their computer, than information will be passed when you call $_COOKIE['user'] and $_COOKIE['pass'].

So, you can always test to see if those variables are not empty, and if they are redirect them to your signin page with the invalid marker.

[PHP]$username = $_COOKIE['user'];
$password = $_COOKIE['pass'];

if(empty($username) || empty($password)){
header('Location: ../signin.php?error=invalid');
}[/PHP]

I'm not sure if this is what you were looking for. If it's not, please correct me.



Posted by: tommyboy123x

i had originally had isset, but it had the same thing, it loads both the "signin.php" and the "index.php"

I think it is more of an issue with the index page because it is 'including' the auth.php page. If i JUST go to the auth page

[php]
UPDATED CODE BELOW
[/php]

This script directs me to google.com, which it should if i have the cookie and my username is in the database... But here is the index.php page with the include(), which i think is giving me the errors

[php]
UPDATED CODE BELOW
[/php]

So why will it ALWAYS load both pages (signin.php and index.php)

btw all the edits have been put in for this thread



Posted by: Babbage

Just to make sure I understand what you are saying, are you saying that everytime you go to then index.php page, no matter if you have the cookies set or not, it will always go to signin.php?

Is there any php code in head.php?



Posted by: tommyboy123x

no it loads both pages... but the index page is on top of the sign in page



Posted by: Babbage

Well, the only way I could see that happening is if your signin.php script calls index.php through an include() or require(). Can you post the code for signin.php?



Posted by: tommyboy123x

I always move the code to the bottom of the thread so that new people who try to help just look at it and say something someone else already did - just "so ya know"

I'm really stumped by this one - i just can't figure out what it is!

Here's signin.php

[php]
<?php
//signin.php
include("head.html"); ?>
</center></font></b>
<font size="3" color="red">
<?php
$error = $_GET['error'];
$signin = $_GET['signup'];

if ($error == invalid){
echo 'Invalid password/username combination';
}
if ($error == sessionerror){
echo 'You are not authorized to view this information or your login information is not valid - Please try again';
}
if ($signin == true){
echo 'Congratulations! You have successfully signed up!';
}
?>
</font><b><font size="4"><center>
Sign-in
<?php include("../../font.html"); ?>
<br><br>Type in your login information. If you do not yet have an account, please <a href="signup.php">request an invite</a>.<br><br>
<form method="POST" action="process.php">
Username:<br>
<input type="text" name="username" size="25" maxlength="20"><br>
Password:<br>
<input type="password" name="password" size="25" maxlength="20"><br>
<input type="submit" value="Sign In" name="submit"></p>
</form>

<?php include("foot.html"); ?>
[/php]

[php]
<?php
//auth.php
$username = $_COOKIE['user'];
$password = $_COOKIE['pass'];

if(empty($username) || empty($password)){
header('Location: ../signin.php?error=sessionerror');
}


//connect to mysql

$hsmemberdb = mysql_connect("**SERVER**", "**USERNAME**", "**PASSWORD**") or die(mysql_error());
mysql_select_db("**DATABASE**") or die(mysql_error());

$query = "SELECT * FROM hsmemberdb WHERE username = '$username' and ********* = '$password'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 1) {
header('Location: h**p://www.google.com'); //edited this url because it was putting the <a> tag around it
exit();
}else{
header('Location: ../signin.php?error=invalid');
}
?>
[/php]

[php]
<?php
//index.php
include('auth.php'); //THIS IS THE FILE FROM ABOVE
include('head.php');

//testing - to make sure the cookies were working
echo 'hello ' .$_COOKIE['user'];
echo '<br />you have '.$_COOKIE['invitekeys'].' invites left';
echo '<br />you have shared '.$_COOKIE['shared'].' files';
echo '<br />you have downloaded '.$_COOKIE['downloaded'].' files';
?>
[/php]



Posted by: Babbage

Well, your code looks like it should be alright to me.

Does it give you any errors when the page loads? Something maybe like "headers have already been modified" or similar?

One thing I did notice, however, is that your index.php includes (head.php) and the signin.php includes (head.html). Don't know if that would make any difference for you, however.



Posted by: tommyboy123x

the head.php is for the members area where it echos the invite codes, viewed, and shared documents - it has a slightly different layout and stuff. I'm going to try and put auth.php within head.php for a test... i'll post how it went

still does it... weirdest thing

but i've decided since the only time someone needs to be a registered user is when performing an action, such as viewing ("downloading") a document or sharing a document, so i'll just figure something out to do it when that occurs.

btw thanks for all the help so far, i noticed you joined about 2 years ago and have accumulated like 1/6 of your posts in this thread. Tech-forums "rediscovered"?



Posted by: Babbage

[QUOTE]the head.php is for the members area where it echos the invite codes, viewed, and shared documents - it has a slightly different layout and stuff. I'm going to try and put auth.php within head.php for a test... i'll post how it went[/QUOTE]

Thats cool. Keep me posted, because I would really like to know what the heck is going on :D You never know when you will run into a problem like this in the future ;)


[QUOTE]btw thanks for all the help so far, i noticed you joined about 2 years ago and have accumulated like 1/6 of your posts in this thread. Tech-forums "rediscovered"?[/QUOTE]

Hey thats no problem.

I wouldn't really say "rediscovered". I come here form time to time, just to see what kind of problems people are having, and if I can, I will try and help them. But when I see a lot of people posting "google search" as answers to serious questions, I get a little discouraged as to how well the forum community works together, so I kind of stray away. But, I still like to show up every now and then to help out.

I'd like to think that if I had a problem, someone would be able to help me out too :)



Posted by: tommyboy123x

you said you wanted me to tell you if I ever found the problem. One line

[php]
session_start()
[/php]

when I did remember to put this in, I didn't put it on the pages I wanted to use a session on. So for example - If i used it, I would have to put it on the signinprocess.php AS WELL AS the index.php in the members page.

I kind of happened upon that and was estatic when i did

EDIT: also, there is no way [that i know of] to get session variables in an included file. You can do this by just changing it to a normal variable though which is what i did



Posted by: Babbage

Glad to hear you were able to figure it out tommyboy123x!

Yeah, usually when I have to use sessions, I put the following in file (something like sessions.php), and just include that in all my files before anything else:

[PHP]<?php
/************************************
* Header Stuff *
************************************/
// This _must_ to be sent BEFORE any HTML
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>[/PHP]


[QUOTE]EDIT: also, there is no way [that i know of] to get session variables in an included file. You can do this by just changing it to a normal variable though which is what i did[/QUOTE]

Did you try storing your information in the $_SESSION[" "] variable? This usually can store session specific information, and as long as you start the session you should be able to access them.



Posted by: tommyboy123x

for some reason when i did something like
[php]
echo 'invite keys: '.$_SESSION['invitekeys'];
[/php]
it showed it as thought it were an empty variable, yet when i store the session variable as something normal, say $invitekeys, it works perfectly.

It may have been a typo because it was a bit late in the night, but i decided it is easier to store it as a regular variable before putting any auth headers and freely accept the extra inefficiency ;-).





vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited.


PPC Management
vB Easy Archive Final - Created by Xenon