Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,214 members, 7,818,728 topics. Date: Sunday, 05 May 2024 at 11:08 PM

How To Create Sessions In PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Create Sessions In PHP (1645 Views)

Online Banking Source Code In PHP, MSQL / How Can I Build A Twitter like @mention System In PHP? / Simple CRUD Application In PHP And Mysql (2) (3) (4)

(1) (Reply) (Go Down)

How To Create Sessions In PHP by Abiodun2014(m): 8:26pm On Apr 16, 2015
I wanna keep my members to my forum logged in for a very long time! Like wen they go off the site, using a mobile browser and come back, dey get logged in automatically!

Anyone can help with the code, I wanna use it for my Nairaland Look alike forum!!!

Would really appreciate if anyone can help
Re: How To Create Sessions In PHP by ritzytbabs(m): 8:37pm On Apr 16, 2015
Abiodun2014:
I wanna keep my members to my forum logged in for a very long time! Like wen they go off the site, using a mobile browser and come back, dey get logged in automatically!

Anyone can help with the code, I wanna use it for my Nairaland Look alike forum!!!

Would really appreciate if anyone can help

have you created the php login form code?
Re: How To Create Sessions In PHP by Nobody: 8:47pm On Apr 16, 2015
cookies
Re: How To Create Sessions In PHP by Abiodun2014(m): 10:31pm On Apr 16, 2015
Yea... For my do_login.php script; the code is posted below

<?php
session_start();
require('incfiles/init.php');
if(functions::isloggedin())
{
if(empty($redirect))
{
functions::go($config->url);
} else {
functions::go('http://'.$redirect);
}
die();
}


if(isset($_POST["submit"])) {
//STEP DOWN VALUES
$username=$_POST["username"];
$password=$_POST["password"];
$redirect=$_POST["redirect"];
//CLEANUP VALUES
$username=functions::cleaninput($username);
$password=functions::cleaninput($password);
$password=md5($password);
$checkquery=mysql_query("SELECT username, password FROM users WHERE username='$username' AND password='$password'"wink or die(mysql_error());
$num=mysql_num_rows($checkquery);
if($num>0)
{
$banq = mysql_num_rows(mysql_query("SELECT * FROM `bannedusers` WHERE `username`='$username' AND `boardid`=0"wink);
if($banq > 0)
{
$binfo = mysql_fetch_array(mysql_query("SELECT * FROM `bannedusers` WHERE username='$username'"wink);
$reason = $binfo["reason"];
$date = $binfo["date"];
$unbandate = $binfo["unbandate"];
$today = time();
if($today < $unbandate) {
$pagetitle = "You Have Been Banished By one of the Administrator!.";
require('incfiles/head.php');
$ud = date('l jS F Y \a\t g:I A', $unbandate);
$bd = date('l jS F Y \a\t g:I A', $date);
echo "<h2>You Have Been Banished By one of the Administrator!.</h2><div class='display'><p>Reason: $reason<p>Banned Date: $bd<p>Unbanned Date: $ud</div>";
require('incfiles/end.php');
die();
} else {
mysql_query("DELETE FROM bannedusers WHERE username='$username' ANd boardid=0"wink;
$pagetitle = "U have Just Been Unbanned";
require('incfiles/head.php');
$msg="U Hav Just Been Unbanned Pls Go Back And Login Again";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}
}

else {
$CookieExpire = 100000;
$time = 60 * 60 * 24 * $CookieExpire;
$time = time() + $time;
$key = $username . date('mY');
$key = strtoupper(md5($key));
setcookie("sessionkey", "$key", "$time"wink;
setcookie("user", "$username", "$time"wink;
setcookie("password", "$password", "$time"wink;

$_SESSION["user"]=$username;
$id=functions::user_info($username, userID); $date=date();
$guestip=$_SERVER["REMOTE_ADDR"];
$guestbrowser=$_SERVER["HTTP_USER_AGENT"];
mysql_query("UPDATE b_guestsonline SET time=1 WHERE browser='$guestbrowser' AND guestip='$guestip'"wink;

mysql_query("UPDATE users SET sessionkey='$key' WHERE username='$username'"wink;
if(empty($redirect))
{
functions::go($config->url);
} else {
functions::go('http://'.$redirect);
}

}
}
else
{
$pagetitle = "incorrect username and/or password, make sure you type correctly your username & password !";
require('incfiles/head.php');
$msg="incorrect username and/or password, make sure you type correctly your username & password !";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}
}
else
{
$pagetitle = "Error";
require('incfiles/head.php');
$msg="Error";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}

require('incfiles/end.php');
?>
Re: How To Create Sessions In PHP by ekenetheorg(m): 9:53am On Apr 22, 2015
Hope you were able to solve your problem Abiodun? If not, whatsapp 07032628111

Abiodun2014:
I wanna keep my members to my forum logged in for a very long time! Like wen they go off the site, using a mobile browser and come back, dey get logged in automatically!

Anyone can help with the code, I wanna use it for my Nairaland Look alike forum!!!

Would really appreciate if anyone can help
Re: How To Create Sessions In PHP by kudaisi(m): 11:15am On Apr 22, 2015
I answered a similar question in this post. https://www.nairaland.com/2261346/urgent-website
Re: How To Create Sessions In PHP by larisoft: 11:31pm On Apr 22, 2015
Abiodun2014:
Yea... For my do_login.php script; the code is posted below

<?php
session_start();
require('incfiles/init.php');
if(functions::isloggedin())
{
if(empty($redirect))
{
functions::go($config->url);
} else {
functions::go('http://'.$redirect);
}
die();
}


if(isset($_POST["submit"])) {
//STEP DOWN VALUES
$username=$_POST["username"];
$password=$_POST["password"];
$redirect=$_POST["redirect"];
//CLEANUP VALUES
$username=functions::cleaninput($username);
$password=functions::cleaninput($password);
$password=md5($password);
$checkquery=mysql_query("SELECT username, password FROM users WHERE username='$username' AND password='$password'"wink or die(mysql_error());
$num=mysql_num_rows($checkquery);
if($num>0)
{
$banq = mysql_num_rows(mysql_query("SELECT * FROM `bannedusers` WHERE `username`='$username' AND `boardid`=0"wink);
if($banq > 0)
{
$binfo = mysql_fetch_array(mysql_query("SELECT * FROM `bannedusers` WHERE username='$username'"wink);
$reason = $binfo["reason"];
$date = $binfo["date"];
$unbandate = $binfo["unbandate"];
$today = time();
if($today < $unbandate) {
$pagetitle = "You Have Been Banished By one of the Administrator!.";
require('incfiles/head.php');
$ud = date('l jS F Y \a\t g:I A', $unbandate);
$bd = date('l jS F Y \a\t g:I A', $date);
echo "<h2>You Have Been Banished By one of the Administrator!.</h2><div class='display'><p>Reason: $reason<p>Banned Date: $bd<p>Unbanned Date: $ud</div>";
require('incfiles/end.php');
die();
} else {
mysql_query("DELETE FROM bannedusers WHERE username='$username' ANd boardid=0"wink;
$pagetitle = "U have Just Been Unbanned";
require('incfiles/head.php');
$msg="U Hav Just Been Unbanned Pls Go Back And Login Again";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}
}

else {
$CookieExpire = 100000;
$time = 60 * 60 * 24 * $CookieExpire;
$time = time() + $time;
$key = $username . date('mY');
$key = strtoupper(md5($key));
setcookie("sessionkey", "$key", "$time"wink;
setcookie("user", "$username", "$time"wink;
setcookie("password", "$password", "$time"wink;

$_SESSION["user"]=$username;
$id=functions::user_info($username, userID); $date=date();
$guestip=$_SERVER["REMOTE_ADDR"];
$guestbrowser=$_SERVER["HTTP_USER_AGENT"];
mysql_query("UPDATE b_guestsonline SET time=1 WHERE browser='$guestbrowser' AND guestip='$guestip'"wink;

mysql_query("UPDATE users SET sessionkey='$key' WHERE username='$username'"wink;
if(empty($redirect))
{
functions::go($config->url);
} else {
functions::go('http://'.$redirect);
}

}
}
else
{
$pagetitle = "incorrect username and/or password, make sure you type correctly your username & password !";
require('incfiles/head.php');
$msg="incorrect username and/or password, make sure you type correctly your username & password !";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}
}
else
{
$pagetitle = "Error";
require('incfiles/head.php');
$msg="Error";
echo "<div class='display'>$msg<br/><a href='javascript:history.go(-1)'>Go back</a></div>";
}

require('incfiles/end.php');
?>

Haba! That is unecessarily long na and even introduces a lot more than was asked.

please use this on login page

//collect username and password:
$username = $_POST['password'];
$password = $_POST['password'];

//...verify and validate your username and password (I guess you know how to do that)


//set cookie
$time = time() + 60 * 60 * 7 * 30;
setcookie("user", $username, $time);
setcookie("password", $password, "$time"wink;

//log user in
}


// now on the index page put

//check to see if user already has a session:
if(isset($_COOKIE['user'])){

//if yes, collect username and password from cookie instead of login form:

$username = $_COOKIE['user'];
$password = $_COOKIE['password'];


//....verify and validate then log user in automatically.

}

Hope this helps. I'm a fan of short clean code.
Re: How To Create Sessions In PHP by Abiodun2014(m): 11:44pm On Apr 24, 2015
larisoft:


Haba! That is unecessarily long na and even introduces a lot more than was asked.

please use this on login page

//collect username and password:
$username = $_POST['password'];
$password = $_POST['password'];

//...verify and validate your username and password (I guess you know how to do that)


//set cookie
$time = time() + 60 * 60 * 7 * 30;
setcookie("user", $username, $time);
setcookie("password", $password, "$time"wink;

//log user in
}


// now on the index page put

//check to see if user already has a session:
if(isset($_COOKIE['user'])){

//if yes, collect username and password from cookie instead of login form:

$username = $_COOKIE['user'];
$password = $_COOKIE['password'];


//....verify and validate then log user in automatically.

}

Hope this helps. I'm a fan of short clean code.

It didn't work oooo
Re: How To Create Sessions In PHP by larisoft: 10:07am On Apr 26, 2015
<?php

$username = $_POST['username']; //corrected this
$password = $_POST['password'];

//...verify and validate your username and password (I guess you know how to do that)


//set cookie
$time = time() + 60 * 60 * 7 * 30;
setcookie("user", $username, $time);
setcookie("password", $password, $time); //corrected this too

//log user in...
}


// now on the index page put

<?php

//check to see if user already has a session; if yes, collect username and password from cookie instead of login form:

if(isset($_COOKIE['user'])){

$username = $_COOKIE['user'];
$password = $_COOKIE['password'];

//verify against database username and password and take user in.
?>



}

And my guy..abeg dey try read code small before you copy paste am. There are three ways to never really learn programming and yet be a programmer. They are
1. Copying and Pasting.
2. Copying and Pasting.
3. Copying and Pasting Ruthlessly
Re: How To Create Sessions In PHP by Abiodun2014(m): 10:18pm On May 07, 2015
larisoft:
<?php

$username = $_POST['username']; //corrected this
$password = $_POST['password'];

//...verify and validate your username and password (I guess you know how to do that)


//set cookie
$time = time() + 60 * 60 * 7 * 30;
setcookie("user", $username, $time);
setcookie("password", $password, $time); //corrected this too

//log user in...
}


// now on the index page put

<?php

//check to see if user already has a session; if yes, collect username and password from cookie instead of login form:

if(isset($_COOKIE['user'])){

$username = $_COOKIE['user'];
$password = $_COOKIE['password'];

//verify against database username and password and take user in.
?>



}

And my guy..abeg dey try read code small before you copy paste am. There are three ways to never really learn programming and yet be a programmer. They are
1. Copying and Pasting.
2. Copying and Pasting.
3. Copying and Pasting Ruthlessly
My nairaland look alike site loads very slow or doesn't load at all on Operamini alone. How can I solve this?!
Re: How To Create Sessions In PHP by Nobody: 3:34am On May 08, 2015
All of you using mysql_connect or mysql_* I don't think it seems to work anymore,
Use pdo or mysqli

For the guy who's asking about a site not loading on opera mini, I suggest you don't open it with that at all, if the browser may have not been updated, it's likely to behave like that.

Have fun
Re: How To Create Sessions In PHP by AAinEqGuinea: 4:55am On May 08, 2015
Love it, love to see brogrammers coming together


I'm starting trekking journey to unite programmers angry
Re: How To Create Sessions In PHP by Abiodun2014(m): 12:50am On May 09, 2015
gimakon:
All of you using mysql_connect or mysql_* I don't think it seems to work anymore,
Use pdo or mysqli

For the guy who's asking about a site not loading on opera mini, I suggest you don't open it with that at all, if the browser may have not been updated, it's likely to behave like that.

Have fun

Since mobile is taking ova now, users can't do without using their operamini browser because of its advantages.
About the mysqli ish can u give me a citation; like an example to illustrate wat u mean! Thanks
Re: How To Create Sessions In PHP by Nobody: 7:13am On May 09, 2015
Abiodun2014:


Since mobile is taking ova now, users can't do without using their operamini browser because of its advantages.
About the mysqli ish can u give me a citation; like an example to illustrate wat u mean! Thanks

Ok I wonna connect to a database for instance
I do something like this


<?php
$hostname = "localhost";
$dbuser = "root";
$dbpass = ""; // no password to connect to db
$db = "users";

$con = mysqli_connect($hostname,$dbuser,$dbpass,$db) or die('cannot connect, error:',.mysql_error());

//get parameters to input data to database
$username = $_POST['username'];
$password = $_POST['password'];
$telephone = $_POST['tel'];

// send data to database
$query = "INSERT INTO People (username,password,tel) VALUES ('".$username."','".$password."','".$telephone."')
mysqli_query($con,$query) or die ('failed query');

echo "1 record inserted ";
mysqli_close($con);

?>



Ok, that's what it looks like. I'm writing from my BlackBerry, am not on computer now, so that's what a mysqli query should look like, hence u reading, try reading PDO too.
Pdo is best for some php guys because they said you just get to change the parameter name for the rdbms and the user and pass, and can be used to connect any database then make something like a prepared statement.

I should believe this helps...
Have fun...

PS : consider using real_escape_string() or stripslashes() to $_POST data, helps you secure your data from mysql injection.

Thanks and God bless you
Re: How To Create Sessions In PHP by Abiodun2014(m): 7:29am On May 09, 2015
gimakon:


Ok I wonna connect to a database for instance
I do something like this


<?php
$hostname = "localhost";
$dbuser = "root";
$dbpass = ""; // no password to connect to db
$db = "users";

$con = mysqli_connect($hostname,$dbuser,$dbpass,$db) or die('cannot connect, error:',.mysql_error());

//get parameters to input data to database
$username = $_POST['username'];
$password = $_POST['password'];
$telephone = $_POST['tel'];

// send data to database
$query = "INSERT INTO People (username,password,tel) VALUES ('".$username."','".$password."','".$telephone."')
mysqli_query($con,$query) or die ('failed query');

echo "1 record inserted ";
mysqli_close($con);

?>



Ok, that's what it looks like. I'm writing from my BlackBerry, am not on computer now, so that's what a mysqli query should look like, hence u reading, try reading PDO too.
Pdo is best for some php guys because they said you just get to change the parameter name for the rdbms and the user and pass, and can be used to connect any database then make something like a prepared statement.

I should believe this helps...
Have fun...

PS : consider using real_escape_string() or stripslashes() to $_POST data, helps you secure your data from mysql injection.

Thanks and God bless you

Thanks a lot would try that now!
Re: How To Create Sessions In PHP by Abiodun2014(m): 7:40am On May 09, 2015
gimakon:


Ok I wonna connect to a database for instance
I do something like this


<?php
$hostname = "localhost";
$dbuser = "root";
$dbpass = ""; // no password to connect to db
$db = "users";

$con = mysqli_connect($hostname,$dbuser,$dbpass,$db) or die('cannot connect, error:',.mysql_error());

//get parameters to input data to database
$username = $_POST['username'];
$password = $_POST['password'];
$telephone = $_POST['tel'];

// send data to database
$query = "INSERT INTO People (username,password,tel) VALUES ('".$username."','".$password."','".$telephone."')
mysqli_query($con,$query) or die ('failed query');

echo "1 record inserted ";
mysqli_close($con);

?>



Ok, that's what it looks like. I'm writing from my BlackBerry, am not on computer now, so that's what a mysqli query should look like, hence u reading, try reading PDO too.
Pdo is best for some php guys because they said you just get to change the parameter name for the rdbms and the user and pass, and can be used to connect any database then make something like a prepared statement.

I should believe this helps...
Have fun...

PS : consider using real_escape_string() or stripslashes() to $_POST data, helps you secure your data from mysql injection.

Thanks and God bless you

It aint working showing can't connect to database
Re: How To Create Sessions In PHP by Nobody: 9:17am On May 09, 2015
Abiodun2014:


It aint working showing can't connect to database

Did u create a database? Or u think copy pasting is what is called programming, a simple code that you have to debug is what is looking at you. I suggest you learn how to program and don't come here to embarrass yourself because of a simple program. The error looking at you can be easily solved if you know your way around mysql phpmyadmin

Or you can echo create database and do the needful, programmers are not always told what to do, they study. So study and program. Thank u

1 Like

(1) (Reply)

Please Help, Browsers Can't Find My External Css / Dear Seun You Are Letting Aspiring Programmers Down! / ...

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 67
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.