PHP - Confused About Mimicing Register_globals On And Destroying All Globals
In the following code,
Code: [Select] // Register Globals if (ini_get('register_globals')) { ini_set('session.use_cookies', 'On'); ini_set('session.use_trans_sid', 'Off'); session_set_cookie_params(0, '/'); session_start(); $globals = array($_REQUEST, $_SESSION, $_SERVER, $_FILES); foreach ($globals as $global) { foreach(array_keys($global) as $key) { unset($$key); } } } the above destroys all globals if register_globals is on, as I understand it. However, if it does destroy all globals, can a web form continue to work? How do you allow form fields and other stuff to be used in a script even if you kill all the globals up front? Many thanks Similar TutorialsWorking making an jquery ajax/php chat system It thought to myself wouldnt it be cool to have commands for admins? Any way I was thinking how the kick command would work. If I keep a list of all the session ID's in a table how can I get PHP to kill a session based on a certain session ID? Say for example some users session ID was 3evirkjonfg5dr4s5b783nvvf0 how can I use that to instantly kill a session so the user is logged out? Here is my code that sets the cookie for a year: //set a cookie for a year setcookie('user', $username, time()+31536000); My code that unsets it: <?php unset($_COOKIE['user']); header("LOCATION:index.php"); ?> Why doesn't it log the user out? Hi, I am facing problems in destroying the session variables in my logout file .I have used session_id() in my php files for ex $sid = session_id().Now when I click on logout I naviagte to my index file but the session variable $sid still remain.I have chkd it.I have kept session_start() function in a file and I am including that file in every php by require_once function.The file is palced in Includes folder..is it causing the problem?? please help me out Also can I use session_start() more than once in a single php file? My logout file is : <?php session_start(); session_destroy(); header("location:index.php"); exit(); ?> When I turn register_global is turned off (register_argc_argv is also set to off), this script won't work. How can I have register_globals turned off and make this work for me. Any help is highly appreciated. <?php $bd_me = mysql_bd_meect("localhost","db_me","pw_me"); mysql_select_db("db_me"); $query =sprintf("SELECT * FROM db WHERE unamename = '%s' AND stus = 'Banned'", mysql_real_escape_string($uname)); $query_new = mysql_query($query); if(mysql_num_rows($query_new)) { $login = "Sorry."; echo($login); } else { $result=sprintf("SELECT * FROM db WHERE unamename = '%s' AND password ='%s'", mysql_real_escape_string($uname), mysql_real_escape_string($password)); $resultf = mysql_query($result); if(mysql_num_rows ($resultf) == 0) { $login = "Failed."; echo($login); } else { $row = mysql_fetch_array($result); $uname=$row['uname']; $password=$row['password']; $login = "$uname=" . $uname . "$password=" . $password . "Successful."; echo($login); } } ?> Is there any other settings that needs to be changed in the php.ini to make this code work while register_globals is turned off? I have script that I need register_globals = On right now I have to put php.ini into root of my webpage on webhost. but that means it turn on register_globals = On for whole site. I have tried just putting php.ini into subdomain but does not work, well script does not work. I have tried to put AddHandler application/x-httpd-php5s .php into the .htaccess in subdomain but that does not work either, script stop working. I can not get script to work if php.ini is in root of webpage webhost. What I'm I doing wrong, thanks hi,
im a php noob trying to echo session variable based on what the user name is but neither is the session initialising nor is it destroying as i can go from index to home page by typing it in the browser with out the user input can any one help me with this(pardon my english) my code is as follows(if im missing something please tutor me on that too as that wud b helpful becoz web resources on sessions r not helpful)
code in home.php:
<?php include '1.1.php'; include '1.php'; ob_start(); session_start(); $_SESSION['uname']=$s1||$b1; ?> <!DOCTYPE html> <html> <head> <title>fetch-array</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </script> <link href="css/hello.css" rel="stylesheet"> <script type="text/javascript" src="tinymce/tinymce.min.js"></script> <script> tinymce.init({ selector: "textarea#elm1", theme: "modern", width: 500, height: 150, plugins: [ "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker", "searchreplace wordcount visualblocks visualchars code media insertdatetime media nonbreaking", "save table contextmenu directionality emoticons template paste textcolor" ], content_css: "css/content.css", toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons", style_formats: [ {title: 'Bold text', inline: 'b'}, {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}}, {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}}, {title: 'Example 1', inline: 'span', classes: 'example1'}, {title: 'Example 2', inline: 'span', classes: 'example2'}, {title: 'Table styles'}, {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'} ] }); </script> </head> <body> <header> <div align="left"><a href="session_stop.php">logout</a></div></header> <br> <div id="container"> <div class="Container-left"> <?php echo $_SESSION['uname'];?> </div>note that it isn't also echoing any session variable too then here is my session stop page: <?php session_start(); session_unset(); session_destroy(); if(session_destroy()==1){ setcookie('', NULL, time()-100,'/'); header('location:index.php');}were am i going wrong??? Hello All! Can anyone explain why when I run the following code <?php print_r($GLOBALS); ?> I get the following output Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) ) which is expected since register_globals is set to off and no variables are defined, but when I add the following code. which is just to echo out a variable from the superglobal $_SERVER, the print_r($GLOBALS) now outputs all the superglobal $_SERVER variables as if echo somehow made all the variables available to the script and now $GLOBALS sees them. maybe a stupid question I don't know. For obvious reasons I can't show the output, but it's easy enough to run the code to view your systems output. <?php print_r($GLOBALS); echo "<br />"; echo "<br />"; echo $_SERVER["DOCUMENT_ROOT"]; ?> Thanks, steadythecourse Hi all, Just moving my old website to a new server and I can't turn register_globals off. The company that designed the site for me has long gone and I don't know enough about php to do this...any help would be greatly appreciated. The error I am getting when trying to access the page is [01-Feb-2011 05:37:13] PHP Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0 [01-Feb-2011 05:37:14] PHP Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'newquayu_uncover'@'localhost' (using password: YES) in /home/xmasphot/public_html/www.newquayuncovered.com/music/classes/DB_class.php on line 44 I have had to change the name of the database and the access details, this has been reflected in other documents but I am guessing that this is trying to use the orignal details somehow??? <? /* Script * Date Created: 07 Jun, 2004 * Last Updated: 07 Jun, 2004 * * DB_class.php */ class DB { //connection parameters var $host = ''; var $user = ''; var $password = ''; var $database = ''; //Database connection handle var $conn = NULL; //Query result var $result = false; var $row; function DB($host,$user,$password,$database,$persistent = true) { $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; $this->perstitent = $perstitent; } function open() { //chose appropriate connect function if($this->perstitent) { $func = 'mysql_pconnect'; } else { $func = 'mysql_connect'; } //connect to mysql server $this->conn = $func($this->host,$this->user,$this->password); if(!$this->conn) { echo mysql_error(); exit; } //select the requested database if(!mysql_select_db($this->database,$this->conn)) { echo mysql_error(); exit; } return true; } function close() { return(mysql_close($this->conn)); } function error() { return(mysql_error()); } function query($sql) { /*if(!$this->result = mysql_query($sql,$this->conn)){ echo mysql_error(); exit; }*/ $this->result = mysql_query($sql,$this->conn); return $this->result; } function fetchobject($result='') { if(!$result){ $result = $this->result; } if(!$res = mysql_fetch_object($result,MYSQL_ASSOC)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function fetcharray() { if(!$res = mysql_fetch_array($this->result,MYSQL_NUM)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function fetchfield() { if(!$res = mysql_fetch_field($this->result)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function numrows($result='') { if(!$result){ $result = $this->result; } if(!$res = mysql_num_rows($result)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function affectedrows() { if(!$res = mysql_affected_rows($this->conn)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function insertid() { if(!$res = mysql_insert_id($this->conn)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function freeresult() { if(!$res = mysql_free_result($this->result)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } function fetchrow($result='') { if(!$result){ $result = $this->result; } if(!$res = mysql_fetch_row($result)){ if(mysql_error() != ""){ echo mysql_error(); exit; } } return $res; } } ?> Why won't this code work when register_globals is turned off (works fine when register_globals is on though). <?php $link = mysql_connect("localhost","un","pw"); mysql_select_db("the_db"); $username = $_POST['usernamex']; $surname = $_POST['surname']; $email = $_POST['email']; $result =sprintf("SELECT * FROM tbl WHERE username ='%s' || email = '%s'", mysql_real_escape_string($username), mysql_real_escape_string($email)); $resultfin = mysql_query($result); if (mysql_num_rows ($resultfin) > 0){ $register = "Retry."; echo($register); } else { $go =sprintf("INSERT INTO tbl (username, email, surname) VALUES ('%s', '%s', '%s'", mysql_real_escape_string($username), mysql_real_escape_string($email), mysql_real_escape_string($surname)); $resultg = mysql_query($go); $register = "Successful."; echo($register); } ?> Thanks, Bickey. i have two SMTP scripts (attached) they need register_globals to be on to work without destroying one of my cookies. how do i modify them so that they dont need register_globals? thank you, sorry for the open ended question, ask me anything to help you help me! thanks. [attachment deleted by admin] hi guys. i am trying to set a variable to the session global variable. I've initialized the session with session_start(); at the very beginning of my website, and then i try finding if a session variable has been set like so if(isset($_SESSION['user'])) { print 'user is logged in'; } but this works in reverse. i have not set any session variables, but asking if it is set, results in an affirmative answer. so i am thinking this is because of register_globals. because i went to read about register_globals, and it says that if register_globals is turned off, i cannot use any other varibale except members of the session array. i dont know what that is yet, but a question befo in the php manual it says register_globals is deprecated, and it appears i need to enable it to set other variables as session variables besides it's associative array. if this is true, how to enable register_globals and allow other variables to be used as session variables? Almost every time when I read about globals, programmers discourage their use. I have a function where I need to send back some variables (which I will use in a query) so I need to use globals. Below is the function and query. I'm trying to figure out if it's ok to use globals in it. function paginate($connection, $tableName) { //the forsaken globals global $limit; global $start; //Pagination $targetpage = "http://localhost/website/untitled2.php"; $limit = 4; //count rows $sql = "SELECT COUNT(*) as num FROM $tableName"; $total_pages = $connection->query($sql) or die(mysqli_error($connection)); $row = $total_pages->fetch_assoc(); $total_pages = $row['num']; //if there's no page number, set it to the first page $stages = 3; $page = isset($_GET['page']) ? $_GET['page'] : 0; $start = empty($page) ? 0 : ($page - 1) * $limit; // Initial page num setup if ($page == 0){$page = 1;} $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages/$limit); $LastPagem1 = $lastpage - 1; $paginate = ''; if($lastpage > 1) { $paginate .= "<div class='paginate'>"; // Previous if ($page > 1){ $paginate.= "<a href='$targetpage?page=$prev'>previous</a>"; }else{ $paginate.= "<span class='disabled'>previous</span>"; } // Pages if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";} } } elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few? { // Beginning only hide later pages if($page < 1 + ($stages * 2)) { for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>"; } // Middle hide some front and some back elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { $paginate.= "<a href='$targetpage?page=1'>1</a>"; $paginate.= "<a href='$targetpage?page=2'>2</a>"; $paginate.= "..."; for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>"; } // End only hide early pages else { $paginate.= "<a href='$targetpage?page=1'>1</a>"; $paginate.= "<a href='$targetpage?page=2'>2</a>"; $paginate.= "..."; for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";} } } } // Next if ($page < $counter - 1){ $paginate.= "<a href='$targetpage?page=$next'>next</a>"; }else{ $paginate.= "<span class='disabled'>next</span>"; } $paginate.= "</div>"; } echo $total_pages.' Results'; // pagination echo $paginate; }//end function and this is how I'm using the function. Without the globals I would get undefined vars $start and $limit used in the query below. paginate($connection, "categories"); $sql = "SELECT * FROM categories ORDER BY cat_name LIMIT $start, $limit"; $cats_result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $cats_result->fetch_assoc()) { $cat_id = $row['cat_id']; $cat_name = $row['cat_name']; $cat_desc = $row['cat_desc']; ...etc Am I using the globals properly? Has anyone encountered this bug which had me banging my head against the desk all morning? In the beginning I registered a $GLOBALS['direction'] that equalled to a radio button value. Later in the script I declare a variable $direction that for some strange reason took the value of $GLOBALS['direction'] without me even writing so. So when i compared them they had the same value. As soon as I changed $direction to $directionx the script worked and the value wasn't "copied" to the $GLOBALS. What's up? What does this mean? Code: [Select] {$GLOBALS['path']} Well I heard that registering $GLOBALS is a bad practice in general since their values can be changed by anyone at anytime. However, the usage of $GLOBALS does simplify the script considerably at times when a certain column in a table needs to be retrieved repeatedly. A good example is user's money data stored in table prefix_users as shown below: Code: [Select] $result = mysql_query( "SELECT * FROM {$prefix}users WHERE uid = '$uid'"); $GLOBALS['usersettings'] = mysql_fetch_array($result); $GLOBALS['money'] = $GLOBALS['usersettings']['money']; If the above code is included in a function file, it will be possible to simply use $GLOBALS['money'] to retrieve user's money data without having to write lines of mysql commands everytime. So I was wondering, is there another way to retrieve database info from a certain column easily but not to register $GLOBALS? Just curious. I WANT TO MAKE AN ARRAY LIKE $GLOBALS structu if you run var_dump($GLOBALS): you will see that this is an array and contains another 9 array with key names. when there is a value(name) in "_POST" you cant use echo $GLOBALS["name"]. but when there is a value("fname") in "GLOBALS" you can use echo $GLOBALS["fname"]. how this array works like that? and how to make an array that behave like that? Edited January 17, 2020 by Silent-BHi I'm currently experiencing problems with my super global outputs.
I'm using
$_SERVER['HTTP_X_MXIT_NICK']; I'm learning functions and I'm working on a rating script. This is a small test script that works, you can try it out yourself: <?php // Rating System function while_test (){ $a = 1; $b = 4; $t_id = 1; global $likes; global $dislikes; global $con_id; while ($a++ <= $b){ echo "<center>"; echo "<table><tr><td>Table: </td></tr>"; echo "<tr><td>This is a table test </td></tr>"; echo "<tr><td><form action='' method='post'>"; echo "<button type='submit' name='likes' value='Y'>likes</button>"; echo "<button type='submit' name='dislikes' value='N'>dislikes</button>"; echo "<input type='hidden' name='hidden_id' value='" . $t_id . "' /></form></td></tr></table>"; echo "</center><br /><br />"; $t_id++; $likes = $_POST['likes']; $dislikes = $_POST['dislikes']; $con_id = $_POST['hidden_id']; } } while_test(); if ($likes) { echo "likes it: " . $likes . " con_id: " . $con_id; } elseif ($dislikes) { echo "dislikes it: " . $dislikes . " con_id: " . $con_id; } ?> I've gotten recommended before not use globals, because the projects would become unmanageable, and I'm wondering how would I be able to avoid using globals in this example? I'm able to in-ject variables through the parenthesis, but I'm not able to out-ject variables, if that makes sense. (?) At least it doesn't work for me. How would I use those three variables $likes, $dislikes and $con_id outside the function without setting them as globals, what would be good practice? Learning something new here so if anyone can tell me why I this wont return a value? page1.php Code: [Select] <?php require "page2.php"; getuserid(); echo $userID; ?> page2.php Code: [Select] <?php function getuserid() { $user =& JFactory::getUser(); $userID = $user->id; global $userID; } ?> This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=356029.0 |