Skip to content

Commit

Permalink
Improve phrasing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maikuolan committed Aug 28, 2023
1 parent 4dd99c3 commit c23b499
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions php/ref/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,47 @@

A session is a way to store information to be used across multiple pages.

Unlike a cookie, the information is not stored on the users computer, It is stored either on a file in the server or in the database (MySQL for example) or Redis.
Unlike a cookie, the information is not stored on the users computer. It is stored either on a file on the server, in the database (MySQL for example), or Redis.

You can access the session variables through the `$_SESSION` global variable.

### Starting the session

You can start the session using `session_start()` function , if it is already started nothing will happen.
You can start the session using the `session_start()` function. If it has already started nothing will happen.

Note : `session_start()` function must be the first thing to be called in the file.
Note : The `session_start()` function must be the first thing to be called in the file.

### Session configurations
### Session configuration

There are many configurations,You can check them in the [Manual](https://www.php.net/manual/en/session.configuration.php) but we will focus on the most important ones.
There are many configuration settings for sessions, and you can check them out in the [manual](https://www.php.net/manual/en/session.configuration.php), but we'll focus on just the most important ones here.

- `session.name` allows you to change the default session name to the name you want.

- `session.auto_start` starts the session automatically, the default is `false`.
- `session.auto_start` starts the session automatically. The default is `false`.

- `session.gc_maxlifetime` sets the maximaum lifetime for the session before invalidating it.

### Session Cookie name
### Session cookie name

You can know the session name by calling `session_name()` function. The default name is `PHPSESSID` but you can change it easily in 4 ways:
You can know the session name by calling the `session_name()` function. The default name is `PHPSESSID`, but you can change it easily in 4 ways:

- In `php.ini` file through `session.name` configuration.
- In your `php.ini` file via the `session.name` setting.

- Through Apache.

- In `.htaccess` file.
- Via your `.htaccess` file.

- Through `ini_set()` function at the start of the file before calling `session_start()` function.
- By using the `ini_set()` function at the start of the file before calling the `session_start()` function.

### Invalidating the session

You can acheive this by doing 3 things:
You can acheive this in 3 ways:

- calling `session_unset()` function which unsets the `$_SESSION` variable.
- By calling the `session_unset()` function which unsets the `$_SESSION` variable.

- Expiring the session cookie in the user's browser:
```php
setcookie(session_name() , '' , time() - 3600);
- By expiring the session cookie in the user's browser:
```PHP
setcookie(session_name(), '' ,time() - 3600);
```

- Calling the `session_destroy()` function.




- By calling the `session_destroy()` function.

0 comments on commit c23b499

Please sign in to comment.