Cara menggunakan php get calling function

Session handling is a key concept in PHP that enables user information to be persisted across all the pages of a website or app. In this post, you'll learn the basics of session handling in PHP.

We'll start with an explanation of how sessions work and how they are related to cookies. Then we'll look at a few code snippets that demonstrate how to work with sessions. You'll learn how to create and destroy sessions, and how to change session variables.

Cookies vs. Session Variables

Not sure if you need cookies or session variables? Session variables are a way to store data about a user in a database and retrieve it later. Cookies are a way to store data about a user on the user's computer. Session variables are typically used in applications that need to keep track of a user's activity. Cookies are typically used in applications that need to store information about a user for a single site.

You can also learn about session variables in my post on using cookies in PHP.

  • Cara menggunakan php get calling function
    Cara menggunakan php get calling function
    Cara menggunakan php get calling function

    How to Work With Cookies in PHP

    Cara menggunakan php get calling function
    Cara menggunakan php get calling function
    Cara menggunakan php get calling function

    Sajal Soni

    16 Feb 2021

What Is a Session in PHP?

A session is a mechanism to persist information across different web pages to identify users as they navigate a site or app. Are you wondering why sessions are needed for a website? To see why sessions are necessary, we have to go back and see how the HTTP protocol is designed to work.

The HTTP protocol is a stateless protocol, which means that there's no way a server can remember a specific user between multiple requests. For example, when you access a web page, the server is just responsible for providing the contents of the requested page. So when you access other pages of the same website, the web server interprets each and every request separately, as if they were unrelated to one another. There's no way for the server to know that each request originated from the same user.

The following diagram depicts the HTTP protocol in a nutshell.

Cara menggunakan php get calling function
Cara menggunakan php get calling function
Cara menggunakan php get calling function

In this model, if you wanted to display user-specific information, you'd have to authenticate a user in each request. Imagine if you had to type your username and password on every page that displayed your profile information! Yes, it would be cumbersome and not practical at all, and that's where sessions come into the picture.

A session allows you to share information across different pages of a single site or app—thus it helps maintain state. This lets the server know that all requests originate from the same user, thus allowing the site to display user-specific information and preferences.

Login Flow With Sessions and Cookies

Let's quickly go through a common login flow for a website to understand what happens behind the scenes.

  1. A user opens the login page of a website.
  2. After submitting the login form, a server on the other end authenticates the request by validating the credentials that were entered.
  3. If the credentials entered by the user are valid, the server creates a new session. The server generates a unique random number, which is called a session id. It also creates a new file on the server which is used to store the session-specific information.
  4. Next, a session id is passed back to the user, along with whatever resource was requested. Behind the scenes, this session id is sent in the 
    3
    47 cookie in the response header.
  5. When the browser receives the response from the server, it comes across the 
    3
    47 cookie header. If cookies are allowed by the browser, it will save this 
    3
    47 cookie, which stores the session id passed by the server.
  6. For subsequent requests, the 
    3
    47 cookie is passed back to the server. When the server comes across the 
    3
    47 cookie, it will try to initialize a session with that session id. It does so by loading the session file which was created earlier, during session initialization. It will then initialize the super-global array variable 
    3
    52 with the data stored in the session file.

In this way, the user data is preserved across multiple requests, and the user is kept logged in throughout a session.

The following diagram depicts how the HTTP protocol works with sessions.

Cara menggunakan php get calling function
Cara menggunakan php get calling function
Cara menggunakan php get calling function

Now that you've seen a brief introduction to how sessions work, we'll create a few practical examples to demonstrate how to create and manipulate session variables.

How to Start a Session

In this section, we’ll discuss how to start a session in PHP.

Whenever you want to deal with session variables, you need to make sure that a session is already started. There are a couple of ways you can start a session in PHP.

Use the 353 Function

This is the method that you'll see most often, where a session is started by the 

3
53 function.

1
<?php
2
// start a session

3
session_start();
4
 
5
// manipulate session variables

<?php
0
<?php
1

The important thing is that the 

3
53 function must be called at the beginning of the script, before any output is sent to the browser. Otherwise, you’ll encounter the infamous 
3
56 error.

Automatically Start a Session

If there’s a need to use sessions throughout your application, you can also opt in to starting a session automatically without using the 

3
53 function.

There’s a configuration option in the php.ini file which allows you to start a session automatically for every request—

3
58. By default, it’s set to 
3
59, and you can set it to 
3
60 to enable the auto startup functionality.

1
<?php
3

On the other hand, if you don’t have access to the php.ini file, and you're using the Apache web server, you could also set this variable using the .htaccess file.

1
<?php
5

If you add the above line in the .htaccess file, that should start a session automatically in your PHP application.

How to Get a Session Id

As we discussed earlier, the server creates a unique number for every new session. If you want to get a session id, you can use the 

3
61 function, as shown in the following snippet.

1
<?php
2
session_start();
3
2
1
4
<?php
1

That should give you the current session id. The 

3
61 function is interesting in that it can also take one argument—a session id. If you want to replace the system-generated session id with your own, you can supply it to the first argument of the 
3
61 function.

1
<?php
2
2
7
3
session_start();
4
<?php
1

It’s important to note that the 

3
61 function must be placed before the 
3
53 call when you want to start a session with a custom session id.

How to Create Session Variables

In this section, we’ll explore how to initialize session variables in PHP.

As we discussed earlier, once a session is started, the 

3
52 super-global array is initialized with the corresponding session information. By default, it’s initialized with a blank array, and you can store more information by using a key-value pair.

Let’s go through the following example script that demonstrates how to initialize session variables.

1
<?php
2
// start a session

3
session_start();
4
 
5
3
1
<?php
0
3
3
3
4
3
5
3
6
 
3
8
3
9
session_start();
0
session_start();
1
session_start();
2
session_start();
3
session_start();
4
<?php
1

As you can see, we’ve started a session at the beginning of the script using the 

3
53 function. Following that, we’ve initialized a couple of session variables. Finally, we’ve accessed those variables using the 
3
52 super-global.

When you store the data in a session using the 

3
52 super-global, it’s eventually stored in a corresponding session file on the server which was created when the session was started. In this way, the session data is shared across multiple requests.

As we discussed, the session information is shared across requests, and thus the session variables initialized on one page can be accessed from other pages as well, until the session expires. Generally, a session expires when the browser is closed.

How to Modify and Delete Session Variables

You can modify or delete session variables created earlier in the application in the same way as for regular PHP variables.

Let’s see how to modify the session variables.

1
<?php
2
session_start();
3
 
4
4
3
5
4
5
<?php
0
4
7
3
4
4
9
3
6
 
1
3
8
4
5
session_start();
0
 
5
session_start();
2
4
9
session_start();
4
 
5
0
5
1
5
2
<?php
1

In the above script, we’ve checked if the 

3
70 variable is set in the first place. If it’s not set, we’ll set it to 
3
60, otherwise we’ll increment it by 
3
60. So, you if refresh this page multiple times, you should see that the counter is incremented by one every time! 

On the other hand, if you would like to delete a session variable, you can use the 

3
73 function, as shown in the following snippet.

1
<?php
2
// start a session

3
session_start();
4
 
5
// manipulate session variables

3
<?php
0
3
3
3
4
 
3
6
// manipulate session variables

9
3
8
<?php
01
session_start();
0
<?php
1

Thus, you can no longer access the 

3
74 variable as it’s deleted by the 
3
73 function. So that’s how you can alter the session information.

How to Destroy a Session

In this section, we’ll see how you could destroy a session. In the previous section, we discussed the 

3
73 function, which is used if you want to delete specific session variables. On the other hand, if you want to delete all session-related data at once, you can use the 
3
77 function.

The 

3
77 function deletes everything that’s stored in the current session. Having said that, it doesn't unset global variables associated with the session or unset the session cookie.

So if you're using the 

3
77 function to log a user out, you must unset the
3
52 variable and unset the session cookie as well. Thus, the recommended way to destroy a session completely is:

1
<?php
2
// start a session

3
session_start();
4
 
5
<?php
13
<?php
0
 
3
4
<?php
17
3
6
<?php
19
3
8
<?php
21
session_start();
0
<?php
23
session_start();
2
4
9
session_start();
4
 
5
0
<?php
29
5
2
<?php
1

Session Handlers

So far, we've discussed how you can perform different operations with session variables. In this section, we'll discuss what a session handler is and how you can use it.

A PHP session handler is a mechanism which instructs PHP how it should manage sessions. The default session handler is a file system, and it means that PHP stores sessions on the disk. Basically, it's a small file on the server which is associated with the unique session id. It's the same id which is stored in a session cookie on the client browser.

The default session handler in PHP provides you with all the features that are needed, but sometimes you want to store sessions differently. For example, you might want to manage sessions in a database, Redis, or some other storage. In this case, you need to implement a custom session handler which overrides the default behavior.

To understand how custom session handlers work, we'll briefly discuss how you can implement a database session handler which manages sessions in a MySQL database.

How to Implement a Database Session Handler

In the PHP session lifecycle, there are different stages like open, read, write, and close. Additionally, there are two more stages: destroy and garbage collection. So when you implement a custom session handler, you have to handle each of these stages to manage the session data properly.

There are two ways you could implement a custom session handler, Either you could define callback functions for different stages in the session lifecycle or you could write a class which implements the

3
81 interface. In both cases, you need to use the 
3
82 function to initialize your custom session handler. In our case, we’ll use the 
3
81 interface implementation.

In our example, we’re going to store sessions in the MySQL database. So let’s create a table which stores the session data by using the following snippet.

1
<?php
33
2
<?php
35
3
<?php
37
4
<?php
39
5
<?php
41
<?php
0
<?php
43

Next, let’s see how our custom database session handler looks:

1
<?php
2
<?php
47
3
4
5
4
<?php
51
5
<?php
0
<?php
54
3
4
<?php
56
3
6
<?php
58
3
8
<?php
60
session_start();
0
session_start();
2
<?php
63
session_start();
4
<?php
56
5
0
<?php
67
5
2
<?php
69
<?php
70
<?php
71
<?php
72
<?php
73
<?php
74
<?php
75
<?php
76
<?php
60
<?php
78
<?php
79
<?php
80
<?php
81
<?php
56
<?php
83
<?php
84
<?php
85
<?php
86
<?php
87
<?php
88
<?php
89
<?php
90
<?php
91
<?php
92
<?php
93
<?php
94
<?php
95
<?php
96
<?php
97
<?php
98
<?php
99
2
00
2
01
2
02
2
03
2
04
<?php
75
2
06
<?php
60
2
08
2
09
2
10
2
11
<?php
56
2
13
<?php
84
2
15
2
16
2
17
2
18
2
19
<?php
90
2
21
<?php
96
2
23
2
24
<?php
69
2
26
2
01
2
28
<?php
73
2
30
<?php
75
2
32
<?php
60
2
34
2
35
2
36
2
37
<?php
56
2
39
<?php
84
2
41
2
42
2
43
<?php
88
2
45
<?php
90
2
47
<?php
96
2
49
2
50
<?php
69
2
52
2
01
2
54
<?php
73
2
56
<?php
75
2
58
<?php
60
2
60
2
61
2
62
2
63
<?php
56
2
65
2
66
2
67
2
68
<?php
84
2
70
2
71
2
72
2
73
2
74
<?php
90
2
76
<?php
96
2
78
2
79
<?php
69
2
81
2
01
2
83
<?php
73
2
85
<?php
75
2
87
<?php
60
2
89
2
90
2
91
2
92
<?php
56
2
94
2
95
2
96
<?php
60
2
98
4
9

Our custom session handler class 

3
84 implements the 
3
81 interface. Thus, it must implement methods that are declared in the 
3
81 interface. We'll look at these methods one by one to understand how each one works.

1
<?php
54
2
<?php
56
3
<?php
58
4
<?php
60

First, to use this code, make sure to replace the 

3
87, 
3
88, and other placeholders with actual values in the 
3
89 method.

1
<?php
63
2
<?php
56
3
<?php
67
4
<?php
69
5
<?php
71
<?php
0
<?php
73
3
4
<?php
75
3
6
<?php
60

When the session is started, the 

3
90 method is called. It returns 
3
91 if the database connection was successful. If there was any problem setting up the database connection, it returns 
3
92.

1
<?php
80
2
<?php
56
3
<?php
84
4
<?php
86
5
<?php
88
<?php
0
<?php
90
3
4
<?php
92
3
6
<?php
94
3
8
<?php
96
session_start();
0
session_start();
2
<?php
99
session_start();
4
2
01
5
0
2
03
5
2
<?php
75
<?php
70
<?php
60

Next, PHP calls the 

3
93 method to read the session data. The 
3
93 method receives the session id as the first argument. We’ll check if there’s any entry available for this session id in the 
3
95 table. If it exists, we’ll return the session data; otherwise, an empty string will be returned.

1
2
10
2
<?php
56
3
<?php
84
4
2
16
5
2
18
<?php
0
<?php
90
3
4
<?php
96
3
6
3
8
<?php
69
session_start();
0
2
01
session_start();
2
<?php
73
session_start();
4
<?php
75
5
0
<?php
60

When PHP needs to save or close a session, it calls the 

3
96 method. It’s used to write the session data in a database. We’ve used the 
3
97 syntax to make sure that if an entry exists, it will be updated; otherwise, it’ll be inserted.

1
2
91
2
<?php
56
3
2
95
4
<?php
60

The 

3
98 method is called after the session 
3
96 method has been called. It works similar to a destructor in classes. In our case, there is nothing particular that needs to be done in the 
3
98 method.

1
2
36
2
<?php
56
3
<?php
84
4
2
42
5
<?php
88
<?php
0
<?php
90
3
4
<?php
96
3
6
3
8
<?php
69
session_start();
0
2
01
session_start();
2
<?php
73
session_start();
4
<?php
75
5
0
<?php
60

The 

session_start();
01 method is called when the session is destroyed with either the 
3
77 or 
session_start();
03 function. In this method, the session data is deleted from a database if it exists.

1
2
62
2
<?php
56
3
2
66
4
5
<?php
84
<?php
0
2
71
3
4
2
73
3
6
<?php
90
3
8
<?php
96
session_start();
0
session_start();
2
<?php
69
session_start();
4
2
01
5
0
<?php
73
5
2
<?php
75
<?php
70
<?php
60

When PHP runs the garbage collector periodically, the 

session_start();
04 method is called. The 
session_start();
05 variable holds the value of the 
session_start();
06 configuration option in the php.ini file. In this method, we’ll delete all sessions that are expired as a part of the garbage collection process.

Using the MySQL Session Handler Class

Now, let’s see how to use the 

3
84 handler class.

1
3
40
2
3
42
3
session_start();
4
3
46

As you can see, we just need to initialize the 

3
84 class and pass it to the 
3
82 function to instruct PHP that it needs to use the 
3
84 class for session management. Next, we’ve called the 
3
53 function to start a session. Finally, we’ve initialized a session variable for testing purposes.

If everything goes well, you should see the session entry in the 

session_start();
12 table as shown in the following screenshot.

Cara menggunakan php get calling function
Cara menggunakan php get calling function
Cara menggunakan php get calling function

And with that, you’ve created a working custom session handler which manages sessions in a database!

Conclusion

In this article, we’ve explored the basics of session handling in PHP. It’s a key concept which allows you to persist information across web pages.

In the first half of the article, we discussed the basic concepts of sessions, and later on we created a few PHP examples to demonstrate how you could create and destroy sessions as well as manipulating session variables.

Bagaimana cara memanggil function?

Cara Memanggil/Eksekusi Fungsi Kita bisa memanggil fungsi di dalam kode Javascript dengan menuliskan nama fungsinya seperti ini: namaFungsi(); Contoh: // membuat fungsi function sayHello(){ console.log("Hello World!"); } // memanggil fungsi sayHello() // maka akan menghasilkan -> Hello World!

Bagaimana langkah pemanggilan fungsi dalam PHP?

Membuat fungsi pada PHP dapat dilakukan dengan mudah, yaitu (1) menuliskan keyword function  (2) kemudian diikuti dengan nama fungsi (3) diikuti dengan tanda kurung ()  sebagai tempat argumen, (4) kemudian diikuti dengan kurung kurawa {} sebagi block statement yang akan dijalankan ketika fungsi dipanggil.

Apa fungsi function PHP?

Fungsi (atau Function) di bahasa pemograman adalah kode program yang dirancang untuk menyelesaikan sebuah tugas tertentu, dan merupakan bagian dari program utama. Kita dapat membuat fungsi sendiri, atau menggunakan fungsi yang dibuat oleh programmer lain.

Jelaskan apa yang dimaksud dengan function?

Fungsi (function) adalah sub modul atau sub program yang dibuat untuk menyelesaikan hal tertentu. Fungsi memiliki nama dan dalam ANSI/C, diimplementasi untuk hal-hal yang khusus dan dapat dipanggil berkali-kali (berulang) dalam program.