/    Sign up×
Community /Pin to ProfileBookmark

well i now have a log in script that grabs the correct pass and makes sure you have the correct pass etc…

but i wanna know what is the best way to do a log in…

should i use cookies… or there are ways in mySQL i think… could i also use like the session function with php?

i am very new to cookies and the session function

what is the best method do you think?

how would i go about coding it in?

thanks!
-chady

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@pyroOct 13.2003 — I would probably go with sessions. See http://us3.php.net/session for more info.
Copy linkTweet thisAlerts:
@chadypuauthorOct 13.2003 — so i would need to add in a session and have it start on log in... and then grab to see if the session is true on each "member only" page

correct?

is there a way i can store the sessions value in a variable... so i can go like

if ($userloggedin)

{

echo 'whatever';

}

thanks pyro
Copy linkTweet thisAlerts:
@pyroOct 13.2003 — Well, you'd need to do it a bit differently than that, but it's close:

When thy log in, use something like this to set the session:

[code=php]session_start();
$_SESSION['loggedin'] = true;[/code]


And then, to check if they are logged in, you'd use this:

[code=php]session_start();
if (isset($_SESSION['loggedin'])) {
#user is logged in
}[/code]
The main thing to remember is that you must start the session (with session_start()) before you can use it.
Copy linkTweet thisAlerts:
@fyrestrtrOct 13.2003 — Read [url=http://www.zend.com/zend/tut/authentication.php] this tutorial[/url] which explains a good deal about authentication.
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — thanks again pyro ?

and ill look at that tutorial
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — Sure thing... ?
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/chadypu/public_html/profile/login.php:10) in /home/chadypu/public_html/profile/login.php on line 49


i got that error

[b]session_start();[/b]

$_SESSION['loggedin'] = true;

the bold line is line 49 :-
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — Make sure you haven't written any content to the browser before you call session_start()
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — thanks again pyro ?

i tried it at the top of my php but not before my html header content

same goes for cookies... you must set the cookie at the top correct?
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — Yes, any header information must be set before output to the browser has been started (including sessions and cookies).
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — all headers except like redirect headers correct?

thanks again
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — Even redirect headers must be set before content is sent to the browser. With PHP 4, however, you can use [URL=http://us2.php.net/manual/en/function.ob-start.php]ob_start()[/URL] and [URL=http://us2.php.net/manual/en/function.ob-end-flush.php]ob_end_flush()[/URL] to get around it.
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — when setting the session how can i include the $uname variable

so on other pages i know what their username is...

how do i put that in the session?

and how could imake the session end... like if they wanted to log out?
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — To set the session, do something like this:

[code=php]$username = "foo";
session_start();
$_SESSION['username'] = $username;
[/code]


And, you use this to end a session:

[code=php]session_start();
session_unset();
session_destroy();[/code]
Copy linkTweet thisAlerts:
@chadypuauthorOct 14.2003 — alright thanks
Copy linkTweet thisAlerts:
@pyroOct 14.2003 — You are welcome... ?
×

Success!

Help @chadypu 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,
)...