PHP - Moved: Unable To Find Bigcommerce Where Global Variables Are Set
This topic has been moved to Third Party PHP Scripts.
http://www.phpfreaks.com/forums/index.php?topic=350034.0 Similar TutorialsThis topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=343041.0 I use $db object to handle database function like $db->get_row for example. Is there any way that I don't need to put into every function global $db or do I actually need to repeat in every function global $db? Code: [Select] function example(){ global $db //Do I need to repeat this in each function? } Hi there, I am a highly concerned about ever using global variables (as many guides I have seen on security issues with PHP say you should avoid them, kind of understand why though to be fair), but I was wanting to really understand custom based session handlers in PHP and this first one he http://uk.php.net/manual/en/function.session-set-save-handler.php on the example: 'Example #1 session_set_save_handler() example' uses them. Now would it be better to take the global variable in function: ''open()' and then return the variable $sess_save_path then pass it up to the next function that may need it perhaps? I just don't want to use globals if I can avoid it, been working out a system that work use for some old solution we use and it uses globals and it really bugs the living life out of me, why people are so lazy they can't avoid them at all costs grrr. Any advice generally speaking is of great appreciation, Jeremy. If I want to load some user information from the database into an object/array and share it throughout my application, what's the best approach for this? Here is what I thought of doing: - I can call a function (e.g user_info() ) that will return the user information whenever I need it , but It'll have to run a DB query each time I call it. - Load user information once from the DB, assign it into a global array/object, then call that object whenever I need it. A lot of people recommended against using global variables, but I think performance wise it's better than running a query each time. Are there any better alternatives than the above approaches? $errors=0; //NEW USER NAME HAS BEEN SUBMITTED, CHECK IT OUT AND THEN ADD IF OKAY $newuser=$_REQUEST['newuser']; function TryAgain(){ global $errors; $errors++; echo '<br/><br/>Please try again <a href="index.php?locate=admin&sub=add_admin">here</a>'; echo "<br/>errors=[$errors]"; } echo "<br/>Trying to add user: $newuser<br/><br/>"; if ($newuser){ }else{ // field was left blank, report this to user and give them option to try again echo '<br/>Field was left empty, you must pick a username.';TryAgain(); } if (strlen($newuser)<5){echo 'Username should be at least 5 characters.';TryAgain();} if (!$errors){echo "continue to SQL [errors=$errors]";} what is wrong with this code? For some reason if I increment $errors with $errors++ within the function it doesn't increment outside the function's scope. even though I declare it as a global at the start of the function. I know I'm doing something stupid here but can somebody please tell me what it is? Thanks I need the following three variables to be available globally, that is to every page. (An common header.html uses them at every page). Please teach me how to initialize them. (inside the header.html maybe?) 'U_M_CODE' => append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode'), 'U_M_TERMS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=terms'), 'U_M_PRV' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=privacy'), Thank you. I have a php script wihich updates data in mysql db named 'trial' and table named 'tab' I am using jquery and ajax to update the db. but the script is not working. edit-profile.php <code> <?php session_start(); $img=$_SESSION['img']; ?> <html> <head> <title>Tgmc</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" media="screen" /> <script type="text/javascript" src="jquery-1.5.min.js"> </script> <script type="text/javascript"> $(function(){ $('#submit').click(function(){ $('#load').append('<img src="ajax-loader.gif" id="loading" alt="image" />'); var about= $('#about').val(); var contact= $('#contact').val(); var present= $('#present').val(); var inter= $('#inter').val(); var high= $('#high').val(); var books= $('#books').val(); var sports= $('#sports').val(); var pastime= $('#pastime').val(); var interest= $('#interest').val(); console.log (about); $.ajax({ url:'update-profile.php', type:'POST', data: 'contact=' + contact + '&about=' + about + '&present=' + present + '&inter=' + inter + '&high=' + high + '&books=' + books + '&sports=' + sports + '&pastime=' + pastime + '&interest=' +interest, success: function(result){ $('#response').remove(); $('#load').append('<p id="response">' + result + '</p>'); $('#loading').fadeOut(500,function(){ $(this).remove(); }); } }); return false; }); }); </script> </head> <body> <!-- start header --> <div id="wrapper"> <div id="header"> <div id="logo"> <h1><a href="#">ABC</a></h1> <p><a href="#">cinemas</a></p> </div> <?php { if(isset($_SESSION['name'])){ echo "<p id=\"welcome\">"; echo "Welcome ".$_SESSION['name']; echo " "; echo "<a href=\"logout.php\">"; echo "Logout"; echo "</a>"; echo "</p>"; } }?> </div> </div> <!-- end header --> <!-- star menu --> <div id="menu"> <ul> <li class="current_page_item"> <?php { if(isset($_SESSION['name'])){ echo "<a href=\"index1.php\">Home</a></li>"; } else{ echo "<a href=\"index.html\">Home</a></li>";}} ?> <li><a href="shows.php">Shows</a></li> <li><a href="umovies.php">Upcoming Movies</a></li> <li><a href="#">Reviews</a></li> <li><a href="#">Book Tickets</a></li> <li><a href="contact.html">Contact</a></li> </ul> </div> <!-- end menu --> <!-- start page --> <div id="page"> <!-- start ads --> <!-- end ads --> <!-- start content --> <div id="content"> <div class="post"> <div class="title"> <h2>My Profile <a href="edit-profile.php">Edit Profile</h2></a> </div> <div class="entry"> <form action="update-profile.php" method="post"> <p style="color:white"><a href="upload.php">Change photo</a><br/> <img src=<?php echo $_SESSION['img'] ?> width='120px' height='140px' class='left' alt='image' /> <strong>Contact:-</strong><input type="text" name="contact" id="contact" value="<?php echo $_SESSION['contact']; ?>" /><br/> About:-<textarea name="about" id="about" rows="5" cols="38" ><?php echo $_SESSION['about']; ?> </textarea><br/> </p> </div> <p style="color:white">Education:<br/> <strong>Present:-</strong> <input type="text" id="present" name="present" size="25" value="<?php echo $_SESSION['present']; ?>" /> <br/> <strong>Inter:- </strong><input type="text" id="inter" name="inter" size="25"value="<?php echo $_SESSION['inter']; ?>" /><br/> <strong>High School:-</strong><input type="text" id="high" name="high" size="25" value="<?php echo $_SESSION['high']; ?>" /><br/> <br><br> <strong>Favourite Books:-</strong><input type="text" id="books" name="books" size="30" value="<?php echo $_SESSION['books']; ?>" /><br/> <strong>Favourite Sport:-</strong><input type="text" id="sports" name="sports" size="30" value="<?php echo $_SESSION['sports']; ?>" /><br/> <br/><br/> <strong>Pastimes:-</strong><input type="text" name="pastime" id="pastime" size="25" value="<?php echo $_SESSION['pastime']; ?>" /><br/> <strong>Interests:-</strong><input type="text" name="interest" id="interest" size="25" value="<?php echo $_SESSION['interests']; ?>" /><br/><br/> <input type="submit" id="submit" value="Update" /> <div id="load></div> </form> </div> <!-- end page --> <!-- start footer --> <div id="footer"> <p class="legal"> ©2010ABC Cinemas. All Rights Reserved. • Design by <a href="#/">Pulkit</a> </p> <p class="links"> <a href="http://validator.w3.org/check/referer" class="xhtml" title="This page validates as XHTML">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a> • <a href="http://jigsaw.w3.org/css-validator/check/referer" class="css" title="This page validates as CSS">Valid <abbr title="Cascading Style Sheets">CSS</abbr></a> </p> </div> </div> <!-- end footer --> </body> </html> </code> update-profile.php <code> <?php session_start(); $connection=mysql_connect("localhost","root",""); mysql_select_db("TRIAL",$connection); if(mysql_query("UPDATE tab SET about='$_POST[about]',contact='$_POST[contact]',present='$_POST[present]',inter='$_POST[inter]',high='$_POST[high]',books='$_POST[books]',sports='$_POST[sports]',pastime='$_POST[pastime]',interests='$_POST[interest]' WHERE name='$_SESSION[name]'")){ echo "Successfully uploaded the data.<a href='index.php'>Click here</a> to return back."; } else echo "Unable to upload"; ?> </code> Hello I've have read that global variables should not be used. so if i have 2 .php files: index.php - main content php file sitefunctions.php - php functions site file on my index.php page i have the following (an example of my problem): <?php displayError($errorID); ?> and in the sitefunctions.php file i have the following (an example of my problem): <?php function displayError($errorID) { if($errorID == 1) { echo "Password Failure"; } else { echo "Other Failure"; } } ?> now that works but since when first accesing that page the $errorID is not set then i get an error. How can i achieve this without first setting a blank $errorID global variable? Thanks Hi, In all my classes, I need to declare globals for everything else, from site variables even to objects (other classes). I obviously don't want to as globals are horrible and I need to have functions clean without globals. I have included a list of all the global vars in the construct and it still doesn't work. Example: start.php <?php $vars = array(some values); $some = more; $variables = foo; $helper = new Helper(); $home = new Home(); This is a little bit simplistic, but for some reason, the variables just don't want to go in the main class without a global. Functions will not run if they require any of these variables. Hello, my global variable is getting whacked (defined null or being undefined) after a function call. I'm not sure if it's happening when leaving the function that assigns it or when the form is "posted", or something else. Code: [Select] <? //global variables $globalVarString = ""; //function definitions function displayResults() { global $globalVarString; if ($globalVarString == "") {displayForm("please enter your search string again");} else {displayForm("you typed: $globalVarString");} } function getResults($searchInput) { global $globalVarString; $globalVarString = $searchInput; //give status ?> <html><head><title>Search</title></head><body> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Thanks for entering your data (<? echo "$globalVarString" ?>).<br><br>Please press the submit button to process your data    <input type="Submit" value="Submit" name="processQuery"> </form></body></html> <? } function displayForm($message) { //prompt for input ?> <html><head><title>Search</title></head><body> <form action=" <? $_SERVER['PHP_SELF'] ?>" method="post"> <h3>Please enter your search <input type="text" name="searchString">    <script type="text/javascript">document.forms[0].searchString.focus();</script> <input type="Submit" value="Submit Query" name="searchQuery"></h3> </form> <? if (!$message == ""){echo "<br>    <h4>" . $message . "</h4><br>";} ?> </body></html> <? } //main() if(array_key_exists('searchQuery', $_POST)) {getResults($_POST['searchString']);} elseif (array_key_exists('processQuery', $_POST)) {displayResults();} else {displayForm("");} ?> This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=356168.0 This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=356577.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=328244.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=321950.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=328966.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=306039.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=319414.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=350064.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=311598.0 |