Toon posts:

[php] sessies.....

Pagina: 1
Acties:
  • 100 views sinds 30-01-2008
  • Reageer

Verwijderd

Topicstarter
Ik heb nu eindelijk een soort inlog php script geschreven dat werkt ;)

maar nu heb ik een vraagje, ik doe dit d.m.v een flat-database uit te lezen want wil het nog niet in MySQL maken :) hoe kan ik veel informatie meenemen naar de volgende pagina? moet ik dan steeds
PHP:
1
2
3
4
5
6
7
<?
session_register("u_name");
session_register("u_pwd");
session_register("u_level");
session_register("u_validated");
// etc
?>

doen? of kan ik ook een array mee sturen? of zijn er andere mogelijkheden?

Verwijderd

Via get of post zou je eventueel veel data mee kunnen sturen

  • chris
  • Registratie: September 2001
  • Laatst online: 11-03-2022
Dit is wel een vraag met een bijzonder hoog RTFM gehalte. Je moet zorgen dat je meer te weten komt over sessies, staat notabenen in de manual.

Maar nou verder met het antwoord:
je moet bovenaan elke pagina dit zetten
PHP:
1
2
3
<?
session_start();
?>

dan kan je de vars als gewone vars aanspreken.
Om ze weer te trashen doe je:
PHP:
1
2
3
<?
session_destroy();
?>
Dit stond in de manual

LXV. Session handling functions
Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

If you are familiar with the session management of PHPLIB, you will notice that some concepts are similar to PHP's session support.

A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

All registered variables are serialized after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later.

The track_vars and register_globals configuration settings influence how the session variables get stored and restored.

Note: As of PHP 4.0.3, track_vars is always turned on.

If track_vars is enabled and register_globals is disabled, only members of the global associative array $HTTP_SESSION_VARS can be registered as session variables. The restored session variables will only be available in the array $HTTP_SESSION_VARS. Example 1. Registering a variable with track_vars enabled

<?php
session_register("count");
$HTTP_SESSION_VARS["count"]++;
?>





If register_globals is enabled, then all global variables can be registered as session variables and the session variables will be restored to corresponding global variables. Example 2. Registering a variable with register_globals enabled

<?php
session_register("count");
$count++;
?>





If both track_vars and register_globals are enabled, then the globals variables and the $HTTP_SESSION_VARS entries will reference the same value.

There are two methods to propagate a session id:


Cookies

URL parameter


The session module supports both methods. Cookies are optimal, but since they are not reliable (clients are not bound to accept them), we cannot rely on them. The second method embeds the session id directly into URLs.

PHP is capable of doing this transparently when compiled with --enable-trans-sid. If you enable this option, relative URIs will be changed to contain the session id automatically. Alternatively, you can use the constant SID which is defined, if the client did not send the appropriate cookie. SID is either of the form session_name=session_id or is an empty string.

The following example demonstrates how to register a variable, and how to link correctly to another page using SID. Example 3. Counting the number of hits of a single user

<?php
session_register ("count");
$count++;
?>

Hello visitor, you have seen this page <?php echo $count; ?> times.<p>

<php?
# the <?=SID?> is necessary to preserve the session id
# in the case that the user has disabled cookies
?>

To continue, <A HREF="nextpage.php?<?=SID?>">click here</A>





The <?=SID?> is not necessary, if --enable-trans-sid was used to compile PHP.

To implement database storage, or any other storage method, you will need to use session_set_save_handler() to create a set of user-level storage functions.

The session management system supports a number of configuration options which you can place in your php.ini file. We will give a short overview.


session.save_handler defines the name of the handler which is used for storing and retrieving data associated with a session. Defaults to files.

session.save_path defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created. Defaults to /tmp.

session.name specifies the name of the session which is used as cookie name. It should only contain alphanumeric characters. Defaults to PHPSESSID.

session.auto_start specifies whether the session module starts a session automatically on request startup. Defaults to 0 (disabled).

session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0.

session.serialize_handler defines the name of the handler which is used to serialize/deserialize data. Currently, a PHP internal format (name php) and WDDX is supported (name wddx). WDDX is only available, if PHP is compiled with WDDX support. Defaults to php.

session.gc_probability specifies the probability that the gc (garbage collection) routine is started on each request in percent. Defaults to 1.

session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and cleaned up.

session.referer_check contains the substring you want to check each HTTP Referer for. If the Referer was sent by the client and the substring was not found, the embedded session id will be marked as invalid. Defaults to the empty string.

session.entropy_file gives a path to an external resource (file) which will be used as an additional entropy source in the session id creation process. Examples are /dev/random or /dev/urandom which are available on many Unix systems.

session.entropy_length specifies the number of bytes which will be read from the file specified above. Defaults to 0 (disabled).

session.use_cookies specifies whether the module will use cookies to store the session id on the client side. Defaults to 1 (enabled).

session.cookie_path specifies path to set in session_cookie. Defaults to /.

session.cookie_domain specifies domain to set in session_cookie. Default is none at all.

session.cache_limiter specifies cache control method to use for session pages (nocache/private/public). Defaults to nocache.

session.cache_expire specifies time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180.

Note: Session handling was added in PHP 4.0.
Suc6!

Verwijderd

Topicstarter
Tnx... nu nog een vraagje: moet ik in een file include moet daar dan ook de array die bv in de login.php staat ook plaatsen of hoeft dat niet?..

inlog.php
PHP:
1
2
3
4
<?
include('include.php');
$u = array("ik", "test");
?>

include.php
PHP:
1
2
3
4
5
6
7
<?
function test() {
    global $u;

    // nu kan ik dus gewoon met de array $u werken?
}
?>

  • chris
  • Registratie: September 2001
  • Laatst online: 11-03-2022
Hmmz, ik snap je vraag niet helemaal. Maar inderdaad, als je een variabele global maakt dan kan je 'm gewoon gebruiken in de functie.

Verwijderd

En volgens mij hoef je (zoals jij het hier codeert) niet eens $u global te maken.

Verwijderd

Topicstarter
welke is het nou?... krijg 2 antwoorden :)

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 09-09 14:51

thomaske

» » » » » »

Ik zou zeggen, Probeer het gewoon even! Is dat zo moeilijk? :)

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


  • Grum
  • Registratie: Juni 2001
  • Niet online
dev-null: post voortaan de link naar de page plz en niet de hele text .. ik heb ook maar modem

Nu even antwoorden:

Je kan ALLES wat je in php kan maken aan variabelen meesturen (int/float etc en ook arrays/objects)

Je kan inderdaad $u gebruiken in de functie met de code die je daar hebt

En tot slot .. wijnolst: nix zeggen voor je et zelf getest hebt :P (et MOET dus met global)

HTH :)

  • Orphix
  • Registratie: Februari 2000
  • Niet online
Je kan met sessies ook objecten gebruiken (instanties van classes). Op die manier hoef je slechts 1 variabele te registeren, namelijk die van je object. En bovendien is je systeem niet zo makkelijk te 'hacken' omdat er geen classes meegegeven kunnen worden aan je script (door middel van de url of bij post-headers).

Verwijderd

Topicstarter
Ik zal beide opties vanmiddag eens proberen.
Op maandag 26 november 2001 04:24 schreef Orphix het volgende:
Je kan met sessies ook objecten gebruiken (instanties van classes). Op die manier hoef je slechts 1 variabele te registeren, namelijk die van je object. En bovendien is je systeem niet zo makkelijk te 'hacken' omdat er geen classes meegegeven kunnen worden aan je script (door middel van de url of bij post-headers).
Hoe bedoel je dit, dat ik een class gebruik?

Verwijderd

Topicstarter
Help!
Mijn code te optimalisteren, tips te geven en etc..

Ik ben bezig met een inlog systeem dat werkt met sessies en de volgende onderdelen zijn al gemaakt (half af)

* login.php
* main.php
* profile.php
* members.php
* showprofile.php?profile=<id>
* filemenu.php (alleen de pagina, geen filemenu nog :P)
* upload.php (alleen de pagina, geen upload nog :P)
* logoff.php (werkt goed :P)

ook heb ik een 'header.php' en een 'profile.inc.php' die geincluded worden :)

zie een voorbeeld op http://www.christianscience.f2s.com/login.php

level: 100
username: admin
password: test

level: ??
username: xtentic
password: login

wie wil mij helpen!.., omdat ik mijn source nog prive wil houden lijkt mij dit de beste oplossing :-)

Verwijderd

Help! Mijn code te optimalisteren, tips te geven en etc..
[...]
omdat ik mijn source nog prive wil houden lijkt mij dit de beste oplossing
Welja, wij zijn ZO ongelooooofelijk goed, dat we jouw code kunnen optimaliseren terwijl je die privé houdt, wat een geweldig respect!

Wat wil je precies weten? Waarmee kunnen we helpen? Dit is te vaag. (E.e.a. werkt wel lekker, ik heb natuurlijk ff gekeken).

Verwijderd

... laat HaaJee en NS nooit alleen, dan gaat ie weer dubbelposten. Excuus ...

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

Op maandag 26 november 2001 zei Xtentic dat de mensen in /38 wel konden helpen, met iets waarvan ze niet eens weten wat er moet gebeuren. Code optimaliseren zonder de code ter beschikking te hebben is volgens hem ook geen punt
:{

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Verwijderd

Topicstarter
Nee, indien iemand zou willen helpen heb ik natuurlijk de source code (linkjes naar .phps) per email klaar liggen :)

Dus kom op, is er iemand die het echt leuk lijkt me tips te geven? etc.

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 09-09 14:51

thomaske

» » » » » »

als je nou die linkjes naar die phps-files ff post, wordt de drempel iets minder groot.. dat er dan veel meer mensen ff een kijkje nemen lijkt me een logisch gevolg.. :)

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


Verwijderd

Topicstarter
sjit, ik krijg de source niet zichtbaar op de f2s server ;(.. als iemand gewoon ff wilt emailen/icqen zou ik dat erg prettig vinden :)

  • Orphix
  • Registratie: Februari 2000
  • Niet online
Op maandag 26 november 2001 06:39 schreef Xtentic het volgende:
Hoe bedoel je dit, dat ik een class gebruik?
ja voorbeeld code:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class SessionData
{
    var $UID;
    var $Level;
    function IsModerator()
    {
     return isset($this->Level) && ($this->Level >= 10);
    }
};

// resume the session
session_start();

// als er nog geen sessie is maken we een instantie van onze class die leeg is
if(!isset($Session))
{
   $Session = new SessionData;
   $Session->UID   = 0;
   $Session->Level = 0;
}

// we willen inloggen, dus de sessie moet bewaard blijven voor de volgende keer
function StartNewSession($UID)
{
   global $Session;

   // 2 uur
   session_set_cookie_params(120);
        
   session_start();
   $Session = new SessionData;
   session_register('Session');

   $Session->UID = $UID;
}

Op het moment dat de gebruikersnaam+wachtwoord goed is, roep je dus de functie StartNewSession aan, met daarbij de gebruikers-id (in mijn geval dan, waarschijnlijk wil je nog andere dingen opslaan)
Pagina: 1