abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/14/2020

What is PHP super global


predefined variables in PHP called "super global", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.



Below are Super global,

1) $GLOBALS

2) $_SERVER

3) $_REQUEST

4) $_POST

5) $_GET

6) $_FILES

7) $_ENV

8) $_COOKIE

9) $_SESSION


1) $GLOBALS - means accessible variables outside of function , for example - $GLOBALS['z'] this variable also accessible outside function 'addition'


<?php 

$x = 75;

$y = 25; 


function addition() {

  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];

}


addition();

echo $z;

?>


2) $_SERVER - 


$_SERVER['PHP_SELF'] - current PHP file name

$_SERVER['GATEWAY_INTERFACE'] - version of the Common Gateway Interface (CGI) currently using

$_SERVER['SERVER_ADDR'] - Host server IP address

$_SERVER['SERVER_NAME'] - name of the host server (such as infyleaf.tech)

$_SERVER['REQUEST_METHOD'] request method used to access the page (such as POST)

$_SERVER['QUERY_STRING'] query string if the page is accessed via a query string

$_SERVER['HTTP_ACCEPT'] Accept header from the current request

$_SERVER['HTTP_ACCEPT_CHARSET'] Accept Charset header from the current request

$_SERVER['HTTP_HOST'] Host header from the current request

$_SERVER['HTTP_REFERER'] complete URL of the current page (not reliable because not all user-agents support it)

$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol

$_SERVER['REMOTE_ADDR'] IP address from where the user is viewing the current page

$_SERVER['REMOTE_HOST'] Host name from where the user is viewing the current page

$_SERVER['REMOTE_PORT'] port being used on the user's machine to communicate with the web server

$_SERVER['SCRIPT_FILENAME'] absolute pathname of the currently executing script

$_SERVER['PATH_TRANSLATED'] file system based path to the current script

$_SERVER['SCRIPT_NAME'] path of the current script

$_SERVER['SCRIPT_URI'] URI of the current page


3) $_REQUEST - get requested name  $name = $_REQUEST['fname']; when form is post


4) $_POST - when post form get value of input for eg -

 $_POST['firstname'], $_POST['mobile']

 

<form action="" method="post">

<input type="text" name="mobile"/>

<input type="submit" value="save"/>

</form>


5) $_GET - get value from url , but should remember not pass secure value like password to this variable.


6)$_FILES - to get value from files when post input type file

basename($_FILES["fileToUpload"]["name"]);


for example 


if ($_FILES["fileToUpload"]["size"] > 2000000) {

  echo "Sorry, your file is too large.";

  $uploadOk = 0;

}


7)$_ENV - get environment variables

<?php

$arr=getenv();

foreach ($arr as $key=>$val)

echo "$key=>$val

";

?>



8) $_COOKIE - get cookie value 


in below example checking if cookie name set or not

<?php

$cookie_name = "user";

$cookie_value = "Mayur";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

?>

<html>

<body>


<?php

if(!isset($_COOKIE[$cookie_name])) {

  echo "Cookie named '" . $cookie_name . "' is not set!";

} else {

  echo "Cookie '" . $cookie_name . "' is set!<br>";

  echo "Value is: " . $_COOKIE[$cookie_name];

}

?>


9) $_SESSION - store value in session , temporary storage over multiple page. 


first line should be session_start();


$_SESSION["favcolor"] = "green";


in case any query please comment


No comments:

Post a Comment

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...