• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP Help Needed!

KidViciou$

Diamond Member
i'm writing a survey application that is using PHP for the front end, and mysql for the back end

i basically am using a php file, login.php to be my "driver"

when a person enters a username and password that is found in the DB, a function userUI() is called to display which surveys they can take.

this is presented using an html form and drop down box. when they select the survey they want, the form logic is called, and if the surveyNum isn't 0 (Reserved for a blank select option), it will call a function takeSurvey()

the problem is, i can no longer access the global $db variable which maintains my database connection. i would have to make a new connection to the database which i'm trying to avoid. i can also no longer access ANY variables in login.php

this is my first time coding php, and i know that using the form is causing this problem, but i don't know how to get around it. any help? TIA
 
Er... ok. You're not exactly providing enough information there, and most of the information you did provide is irrelevent. Is ALL of this action taking place in login.php through multiple postbacks to the server? And are you trying to use variables that you set on previous trips to the server? You can't do that. And if you're tyring to use "global" variables within functions, you have to indicate that you're using a global at the top of the function by using this statement:

global $db;
 
it's important to understand the life cycle of php pages. Once you've created your php page and sent the output, everything from that instance of php is lost. If you want to save anything you need to save them in the $_SESSION `superglobal` array after calling session_start().

So after you're login.php successfully matches a user and generates all the html w/ the choice, all of the variables in login.php cease to exists. THere's no way you can get to any of those, except if you saved them in the $_SESSION array.

I generally have a utility file w/ the following code that I include from all of my files


// Parameters for Web Server
$uname="...";
$pass="...";
$host="localhost";
$dbname="...";
$conn=0;

function conn(){

global $uname,$pass,$host,$dbname,$conn;
$conn=@mysql_pconnect($host,$uname,$pass);
$sel=@mysql_select_db($dbname);
//returns false if failed or connection link (can be treated as true)
return (($conn && $sel) ? $conn : false);
}

function reconn(){
global $conn;
if(! $conn)
return ($conn=conn());
}



And I call conn() or (reconn() -- if i'm not sure if conn() had been called before)... You can't avoid creating a database conneciton each time a script runs even if it's the same session (i'm pretty sure here, correct me if i'm wrong).

But yeah, give us more info and we'll be able to help you out.
 
i have some debugging code in there, like under userUI form handling, i don't actualyl call userUILogic so it won't call takeSurvey()

i was trying to see the scope of global variables, and as i expected, it's like when i use a form, it does a redirect and i lose all my globals
 
this is presented using an html form and drop down box. when they select the survey they want, the form logic is called, and if the surveyNum isn't 0 (Reserved for a blank select option), it will call a function takeSurvey()


Don't understand what you mean here.
After login.php:userUI() is called it will generate the HTML and login.php will complete execution. Then anything that's done inside the generated HTML must call another php file (i.e. submit a form to say takeSurvey.php). The takeSurver.php should be able to figure out surveyNum from the submitted info and create the surver.
You might have to use hidden fields to pass values like the surveynum. I.e. something like:

<input type="hidden" name="hidSurvey" value=<?php echo "'$survey_num'"; ?> />



Ideally you should save the username for the session in a session variable so that once you recieve a submission from takeForm you know that it's part of a session, and you can creat the form for that session.
 
well it's looking like i do have to use sessions

but let me clarify myself. login will call userUI() which presents the drop down box. once a survey is selected, userUI() will call takeSurvey() (the form handler is what makes the call)
 
1. If you want any kind of security, you shouldn't be storing passwords in the database in plaintext format. At least use MD5.
2. You need to use the statement I listed above (global $db😉 for all of the "global" variables you want to use within adminUI(), userUI(), etc. Just put that at the top of the function body.
 
so i started to use sessions, but when i do function calls, the session variables aren't there!

Ex.

login.php:
session_start();
session_register("temp);
$temp = "This is a test!";

test();


test.php:
function test()
{
session_start()
session_register("temp")

echo "$temp";
}





even if i keep the session_start() outside of the function but inside the php tags at the top of the file, it STILL doesn't work in the function. it will however work out of the function, ex. i add a redirect statement in the function to direct to the php page, it will start the session and variables will be there.


basically, sessions aren't working in functions!

 
Try this:

login.php:
session_start();
$_SESSION['temp'] = "This is a test!";

test();


test.php:
function test()
{
session_start()

echo "$_SESSION['temp']";
}
 
jesus christ, i dont' believe it. the $_SESSION part worked

but i try to printout the sessionid from test after starting the session, but get null even though i can acess session variable temp
 
Back
Top