View Full Version : session>> need help!
auriaks
08-06-2009, 10:45 PM
Maybe someone can teach how to create session and where it must be placed in pages... i want, that not registered people cant access other pages.
I will be thankfull for your help :)
forum_amnesiac
08-07-2009, 08:05 AM
This is a good link to show you how to use SESSIONS, use the Next button to read all of it.
http://php.about.com/od/advancedphp/ss/php_sessions.htm
rm_dynamicdrive
08-07-2009, 08:11 AM
if you want good php login download this I am the author of that one I can assist you on that. http://sourceforge.net/projects/phploginsystemw/
rm_dynamicdrive
08-07-2009, 08:20 AM
if you want another visit evolt.org http://evolt.org/node/60384
auriaks
08-07-2009, 11:39 AM
thanks... that may help me :D but it seems to difficult to me. I dont know how to use your system. What i have to change? to fit my web?
<i have login system, but yours seems better>
JShor
08-07-2009, 06:56 PM
It's easier than it seems. Additionally, if SESSION is too complex for you, you can use COOKIE instead to read logins -- not as effective, but it'd getcha by.
actually, my two cents would go for SESSION being a lot simpler (also quicker, less complex, more straightforward, and more effective) than cookies.
auriaks
08-07-2009, 08:30 PM
if i pulled all your files in my database... and change server, db and log, pass, It will work?? Also i want to ask, if i need to create database, table and forms in phpmyadmin??
I have to create as its written in your files?
<i really like your system... i want to use it> :D
JShor
08-07-2009, 10:57 PM
IMO, this is the best place to learn to use session:
http://w3schools.com/php/php_sessions.asp
If you give us the page codes [the registration, login, and pages that you want only registered users to access] we can show you where SESSION belongs.
auriaks
08-07-2009, 11:14 PM
i have just login yet... this is login and maybe you can help me with register also? :confused:
<?php
$host = "server";
$username = "asasas";
$password = "tututu";
$db_name = "phpacademy";
$tbl_name = "members";
mysql_connect($host, $username, $password) or die(mysql_error("can't connect"));
mysql_select_db($db_name) or die(mysql_error());
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1) {
session_register("myusername");
session_register("mypassword");
$message = "Success!";
}
else {
$message = "Wrong Username or Password";
}
?>
<html>
<head>
<title>login</title>
</head>
<body>
message:
<?php echo $message; ?>
</body>
</html>
and this is a page i want to protect...
<html>
<head>
<title>Forum</title>
<!---------------------------------------------------------------->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="drilldownmenu.css" />
<script type="text/javascript" src="drilldownmenu.js">
</script>
<script type="text/javascript">
var mymenu=new drilldownmenu({
menuid: 'drillmenu1',
breadcrumbid: 'drillcrumb',
persist: {enable: true, overrideselectedul: true}
})
</script>
<!---------------------------------------------------------------->
</head>
<body bgcolor="black" text='#000000' link='black' atext='#000000' vlink='black'>
<table width='500px' height='40px'>
<tr>
<td>
</td>
</tr>
</table>
<table background="images/general.JPG" align='center' width='765px' height='440px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<table align='left' width='200px' height='300px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<table>
<tr>
<td>
</td>
</tr>
</table>
<table width='150px' height='200px' align='left'>
<tr>
<td>
<!-----------------------MENU--------------------------------------->
<div id="drillmenu1" class="drillmenu">
<ul>
<li><a href="">Minas Tirith</a></li>
<li><a href="members.php">Guild members</a></li>
<li><a href="forum.php">Forum</a></li>
<li><a href="killlist.php">Kill list [Pk]</a></li>
<li><a href="#">Information</a>
<ul>
<li><a href="#">Empty...</a></li>
<li><a href="#">Empty...</a></li>
<li><a href="#">Empty...</a>
</li>
</ul>
</li>
<li><a href="#">Other guilds</a>
<ul>
<li><a href="enemies.php">Enemies</a></li>
<li><a href="allies.php">Allies</a></li>
</ul>
</li>
<li><a href="traitors.php">!!!TRAITORS!!!</a></li>
<li><a href="#">Shop</a>
<ul>
<li><a href="buy.php">Buy</a></li>
<li><a href="sell.php">Sell</a></li>
<li><a href="rps.php">RPS shop</a>
<ul>
<li><a href="https://shop.playrohan.com/ItemMall/ItemMall.html">Item mall</a></li>
<li><a href="https://shop.playrohan.com/Exchange/Exchange.html">Exchange</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="http://www.playrohan.com">Play Rohan</a></li>
<li><a href="choosen.php">Choosen RooM</a></li>
</ul>
<br style="clear: left" />
</div>
<!-------------------------MENU------------------------------------->
<br><br><br>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table align='right' width='500px' height='50px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<p><font size="5" color='#ECE5B6' face="Old English Text MT"><b>Minas Tirith guild site</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
<!-------------------------langas------------------------------------->
<table background="images/members.jpg" align='right' width='500px' height='360px' style='border-color':black;border-style:solid;borderw-width:0px border='1'>
<tr align='center'>
<td align='center'>
<p><font size="2" color='black' face="Verdana"><b>Sorry... Can't connect</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
<!-------------------------langas------------------------------------->
</td>
</tr>
</table>
<table align='center' width='500px' height='120px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td align='center'>
</td>
</tr>
</table>
</body>
</html>
thanks...
JShor
08-08-2009, 04:14 PM
OK, we don't use session_register(); in modern versions of php anymore. Additionally, you need to start the session on each page you want to protect, and use $_SESSION to register vars.
<?php
$host = "server";
$username = "asasas";
$password = "tututu";
$db_name = "phpacademy";
$tbl_name = "members";
mysql_connect($host, $username, $password) or die(mysql_error("can't connect"));
mysql_select_db($db_name) or die(mysql_error());
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1) {
session_start();
$_SESSION["myusername"];
$_SESSION["mypassword"];
$_SESSION[$myusername];
$_SESSION[$mypassword]; // you need to declare what sessions are storing also
$message = "Success!";
}
else {
$message = "Wrong Username or Password";
}
?>
<html>
<head>
<title>login</title>
</head>
<body>
message:
<?php echo $message; ?>
</body>
</html>
<?php
session_start();
if (isset($_SESSION['myusername'])){
} else {
header("Location: login.php"); // you can chg this to the login pg uri
?>
?>
<html>
<head>
<title>Forum</title>
<!---------------------------------------------------------------->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="drilldownmenu.css" />
<script type="text/javascript" src="drilldownmenu.js">
</script>
<script type="text/javascript">
var mymenu=new drilldownmenu({
menuid: 'drillmenu1',
breadcrumbid: 'drillcrumb',
persist: {enable: true, overrideselectedul: true}
})
</script>
<!---------------------------------------------------------------->
</head>
<body bgcolor="black" text='#000000' link='black' atext='#000000' vlink='black'>
<table width='500px' height='40px'>
<tr>
<td>
</td>
</tr>
</table>
<table background="images/general.JPG" align='center' width='765px' height='440px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<table align='left' width='200px' height='300px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<table>
<tr>
<td>
</td>
</tr>
</table>
<table width='150px' height='200px' align='left'>
<tr>
<td>
<!-----------------------MENU--------------------------------------->
<div id="drillmenu1" class="drillmenu">
<ul>
<li><a href="">Minas Tirith</a></li>
<li><a href="members.php">Guild members</a></li>
<li><a href="forum.php">Forum</a></li>
<li><a href="killlist.php">Kill list [Pk]</a></li>
<li><a href="#">Information</a>
<ul>
<li><a href="#">Empty...</a></li>
<li><a href="#">Empty...</a></li>
<li><a href="#">Empty...</a>
</li>
</ul>
</li>
<li><a href="#">Other guilds</a>
<ul>
<li><a href="enemies.php">Enemies</a></li>
<li><a href="allies.php">Allies</a></li>
</ul>
</li>
<li><a href="traitors.php">!!!TRAITORS!!!</a></li>
<li><a href="#">Shop</a>
<ul>
<li><a href="buy.php">Buy</a></li>
<li><a href="sell.php">Sell</a></li>
<li><a href="rps.php">RPS shop</a>
<ul>
<li><a href="https://shop.playrohan.com/ItemMall/ItemMall.html">Item mall</a></li>
<li><a href="https://shop.playrohan.com/Exchange/Exchange.html">Exchange</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="http://www.playrohan.com">Play Rohan</a></li>
<li><a href="choosen.php">Choosen RooM</a></li>
</ul>
<br style="clear: left" />
</div>
<!-------------------------MENU------------------------------------->
<br><br><br>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table align='right' width='500px' height='50px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td>
<p><font size="5" color='#ECE5B6' face="Old English Text MT"><b>Minas Tirith guild site</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
<!-------------------------langas------------------------------------->
<table background="images/members.jpg" align='right' width='500px' height='360px' style='border-color':black;border-style:solid;borderw-width:0px border='1'>
<tr align='center'>
<td align='center'>
<p><font size="2" color='black' face="Verdana"><b>Sorry... Can't connect</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
<!-------------------------langas------------------------------------->
</td>
</tr>
</table>
<table align='center' width='500px' height='120px' style='border-color':black;border-style:solid;borderw-width:0px border='0'>
<tr>
<td align='center'>
</td>
</tr>
</table>
[PHP]
JShor
08-08-2009, 04:23 PM
Register page
<?php
session_start();
if(isset($_SESSION["myusername"])) {
header("Location: index.php"); // if is already reg/logged in
}
session_destroy();
if(isset($_POST[submit])) {
$user = mysql_query("SELECT * FROM users WHERE username='$_POST[username]'") or die(mysql_error());
if($_POST[password] == $_POST[confirm]) {
if(mysql_num_rows( $user ) > 0) {
echo "This user already exists";
} else {
mysql_query("INSERT INTO users (username, password) VALUES('$_POST[username]', '$_POST[password]') ") or die(mysql_error());
echo "You've successfully registered!";
}
} else {
echo "Your passwords don't match";
}
}
?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="post">
<b>Username:</b>
<input type="text" name="username" value="<?php echo $_POST['username']; ?>"><br>
<b>Password:</b>
<input type="password" name="password"><br>
<b>Confirm:</b>
<input type="password" name="confirm"><br>
<p>
<input type="submit" name="submit" value=" Register ">
<input type="reset" value=" Reset ">
</form>
</body>
</html>
I don'tknow what your registration fields, so I only assumed username and password,but the other fields are easy to implement.
HTH:)
auriaks
08-08-2009, 07:14 PM
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
This is the error of register... what i have to change to mine MySQL information?
JShor
08-09-2009, 03:22 PM
Right, sorry, you need to call the db to connect to it first. Insert this into the beginning of your registration PHp code:
<?php
$host = "server";
$username = "asasas";
$password = "tututu";
$db_name = "phpacademy";
$tbl_name = "members";
mysql_connect($host, $username, $password) or die(mysql_error("can't connect"));
mysql_select_db($db_name) or die(mysql_error());
?>
fg123
08-10-2009, 12:52 AM
ok, i think i can help you with that.
i'm also going to use some scripts from my login system.
ok:
login page
<?php session_start();?>
<html>
<head><title>Login</title></head>
<body>
<!--Login form-->
<form action="process.php" method="get">
<h2>Login</h2>
<label>Username: </label><br />
<input name="user_name" type="text" />
<label>Password: </label><br />
<input name="password" type="password" />
<input type="submit" value="Log In!">
</form>
</body></html>
thats it for login.php
___________________
create a member database with these fields:
loginName , varchar(50)
pass, varchar(255)
createDate, datetime
_________________
call the database "member" without the quotes.
heres process.php
<?php
session_start();
$mysql_host = "localhost";
$mysql_database = "member";
$mysql_user = "username";
$mysql_password = "password";
$cxn=mysql_connect($mysql_host,$mysql_user,$mysql_password);
mysql_select_db($mysql_database);
$rec=mysql_fetch_array(mysql_query("SELECT * FROM member WHERE loginName='$_GET[user_name]' AND pass = '$_GET[password]'"));
if(($rec['loginName']==$_GET["user_name"])&&($rec['pass']==$_GET["password"]))
{
//do what you want to do if the user login is correct.
$_SESSION['auth'] = "yes";
$_SESSION['loginName'] = $_GET['user_name'];
header("Location:member page url");
?>
________________________________________
on top of every page that you want to protect, insert this:
<?php session_start();
if(@$_SESSION["auth"]!= "yes"){header("Location:login.php");}?>
________________________________________
thats it for the login tutorial, read my next post for a registration tutorial.
See if it helps!
fg123
08-10-2009, 12:54 AM
oh well, i guess you dont need a registration tutorial.
i type all that for nothing...
auriaks
08-10-2009, 01:15 AM
Eureka!!! guestbook WORKS!!! :) thanks.. again :) also i want to ask about this session system... it looks good and i understand it, but i created database with table: members. in it are password and username (exactly as i wrote). What i have to change then? (p.s. you starting new session in every page or just in login)
JShor
08-10-2009, 01:48 PM
Chg table members to member, and then within it chg username to loginName and password to pass as per how fg123 wrote the code.
JShor
08-10-2009, 01:52 PM
Actually, it's easier just to DROP table members, and then copy+paste this SQL query into your mysql admin area:
CREATE TABLE `test`.`members` (
`id` INT NOT NULL ,
`loginName` VARCHAR( 50 ) NOT NULL ,
`pass` VARCHAR( 255 ) NOT NULL ,
`createDate` DATETIME NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB
auriaks
08-11-2009, 12:26 AM
about sessions... you wrote
<?php session_start();
if(@$_SESSION["auth"]!= "yes"){header("Location:login.php");}?>
that means i have to start session in every page i want to make safe?
then, is it nessesary symbol @???
And finally, process.php ypu wrote me... i using that, but didnt see where is action which directs to process.php... THANKS
fg123
09-04-2009, 08:34 PM
Yes you have to start session on every page.. And the @ sign is required...I think. And the form in login.php goes to process.php.
fg123
09-04-2009, 08:36 PM
Chg table members to member, and then within it chg username to loginName and password to pass as per how fg123 wrote the code.
You can also change the code to match your database.
fg123
09-04-2009, 10:14 PM
Actually, it's easier just to DROP table members, and then copy+paste this SQL query into your mysql admin area:
CREATE TABLE `test`.`members` (
`id` INT NOT NULL ,
`loginName` VARCHAR( 50 ) NOT NULL ,
`pass` VARCHAR( 255 ) NOT NULL ,
`createDate` DATETIME NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB
why do you need to create `test` table?
auriaks
09-04-2009, 11:04 PM
Thanks, i think im done with guestbook... it works great. for now its ok... maube later ill make some improvements. Thx :)
fg123
09-04-2009, 11:12 PM
You're welcome! :)
JShor
09-04-2009, 11:40 PM
Ignore `test`. that's my database name.
fg123
09-05-2009, 12:18 AM
Oh...Ok. I get you.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.