/    Sign up×
Community /Pin to ProfileBookmark

Hello guys,

I’m building a website which requires the user to log in for extended periods of time (Think something like facebook). Currently I have it setup where the user and login and logoff. I am planning to introduce cookies so that I can identify the user while he / she surfs through the site. I have two questions:

  • 1.

    Should I be using cookies or sessions? Or should I use cookies for the login part then sessions after?

  • 2.

    How often should I open connection to the database server? Should I maintain a close connection at the end of every page so that I don’t end up with a gazillion open connections?

    Thank you so much.

  • Regards,
    Pembar

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @HHCOwnerJul 01.2009 — Hi Pembar,

    [I][B]For your first question:[/B][/I]

    In my personal experience with php programming, I've found that sessions work the best for everything... I will probably never use cookies again, simply because their unreliable to always work.

    [I][B]For your second question:[/B][/I]

    How often you open the connection to the db depends on how many people will be visiting your site; The more people you have, the more often to open the connection. Also I'd close the connection on every page just to be on the safe side.

    Hope this helps. ?
    Copy linkTweet thisAlerts:
    @SyCoJul 02.2009 — The only use for cookies is to hold information over in a browser between visits. The 'remember me' checkbox that keeps you logged in uses a cookie. Sessions also use cookies that expire when the browser closes (or times out) but can store only enough info to identify you to reference all the rest. For you app it sounds like sessions would work just fine and are way easier to work with.

    When you use a function it's a really good idea to read the php.net page entry for it. I've been using PHP professionally for 10 years and still check the man page regularly. Things change, notes are added etc. It's just good practice to read the man pages from time to time, especially if you're new to programming.

    From the man page for the function mysql_connect()

    Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close(). [/QUOTE]

    If it's a simple grab and display then by closing it you'll only save a tiny amount of resource but lose nothing. A lot of people don't bother and don't see any impact on performance. It would be appropriate to close it if you think the SQL resources will be stretched especially if the script has a lot of processing of the information it retrieved from the database.
    ×

    Success!

    Help @Pembar spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...