| | |
PHP Sessions
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello guys,
I'm creating my own website-it's coming along very well but I'm stuck. There is a login, so that you can login to your account. Each account has an access of 1-10, if your access is 0 you are not logged in. When you put your username and password it checks all of the accounts in the mySQL database. That all works fine, but along with this is a forum, if you are logged in you can post ext.. in the forum, if your not logged in you can only view it. I made it so you can view it but how would i have a variable that worked through all the pages in my website so the forum could say do they have an access of greater than 0?
So i thought... Sessions
My default.php runs includes the different things which include each other and it all works out so that its always "technically" on default.php.
I'm not sure if I just don't know how to use Session variables or I'm using them wrong.
here is login.php
Thanks and i hope you guys can help me..
I'm creating my own website-it's coming along very well but I'm stuck. There is a login, so that you can login to your account. Each account has an access of 1-10, if your access is 0 you are not logged in. When you put your username and password it checks all of the accounts in the mySQL database. That all works fine, but along with this is a forum, if you are logged in you can post ext.. in the forum, if your not logged in you can only view it. I made it so you can view it but how would i have a variable that worked through all the pages in my website so the forum could say do they have an access of greater than 0?
So i thought... Sessions
My default.php runs includes the different things which include each other and it all works out so that its always "technically" on default.php.
PHP Syntax (Toggle Plain Text)
<?php // Start the session session_start(); $_SESSION['Username'] = ""; $_SESSION['Access'] = 0; ?> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Website</title> </head> <body> <table width="100%" height="100%" border="0"> <tr height="1%"> <td> <?php require "menubar.php"; ?> </td> </tr> <tr height="10%"> <td> <?php require "logo.php"; ?> </td> </tr> <tr valign="top"> <td> <?php require "main.php"; ?> </td> </tr> </table> </body> </html>
I'm not sure if I just don't know how to use Session variables or I'm using them wrong.
here is login.php
PHP Syntax (Toggle Plain Text)
<html> <body> <?php if(strlen($_POST['user']) == 0 || strlen($_POST['pass']) == 0) { echo " <center> <form action=\"?page=Login\" method=\"post\"> <table border=\"0\" width=\"75%\"> <tr> <td> Username: </td> <td> <input type=\"text\" name=\"user\" /> </td> </tr> <tr> <td> Password: </td> <td> <input type=\"password\" name=\"pass\" /> </td> </tr> <tr> <td> <input type=\"submit\" value=\"Login\" /> <td> </tr> </table> </form> </center> "; } else { $mysql_host = ""; $mysql_database = ""; $mysql_user = ""; $mysql_password = ""; $con = mysql_connect($mysql_host,$mysql_user,$mysql_password); if(!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($mysql_database,$con); $result = mysql_query("SELECT * FROM Accounts"); while($row = mysql_fetch_array($result)) { if($row['Username'] == $_POST['user'] && $row['Password'] == $_POST['pass']) { $_SESSION['Username'] = $row['Username']; $_SESSION['Access'] = $row['Access']; echo "<a href=?page=UserCP>You have successfully logged in click here to access User CP.</a>"; } } } ?> </body> </html>
Thanks and i hope you guys can help me..
0
#2 33 Days Ago
Try making this default.php
And this login.php
I will have to macgyver a login tutorial which reminds me I need to upload that other video tutorial tonight. Hope that helps.
php Syntax (Toggle Plain Text)
<?php // Start the session session_start(); if (empty($_SESSION['Username']) || !isset($_SESSION['Username'])) { $_SESSION['Username'] = ""; $_SESSION['Access'] = 0; } ?> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Website</title> </head> <body> <table width="100%" height="100%" border="0"> <tr height="1%"> <td> <?php require "menubar.php"; ?> </td> </tr> <tr height="10%"> <td> <?php require "logo.php"; ?> </td> </tr> <tr valign="top"> <td> <?php require "main.php"; ?> </td> </tr> </table> </body> </html>
And this login.php
php Syntax (Toggle Plain Text)
<?php session_start(); ?><html> <body> <?php if(strlen($_POST['user']) == 0 || strlen($_POST['pass']) == 0) { echo " <center> <form action=\"?page=Login\" method=\"post\"> <table border=\"0\" width=\"75%\"> <tr> <td> Username: </td> <td> <input type=\"text\" name=\"user\" /> </td> </tr> <tr> <td> Password: </td> <td> <input type=\"password\" name=\"pass\" /> </td> </tr> <tr> <td> <input type=\"submit\" value=\"Login\" /> <td> </tr> </table> </form> </center> "; } else { $mysql_host = ""; $mysql_database = ""; $mysql_user = ""; $mysql_password = ""; $con = mysql_connect($mysql_host,$mysql_user,$mysql_password); if(!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($mysql_database,$con); $result = mysql_query("SELECT * FROM Accounts"); while($row = mysql_fetch_array($result)) { if($row['Username'] == $_POST['user'] && $row['Password'] == $_POST['pass']) { $_SESSION['Username'] = $row['Username']; $_SESSION['Access'] = $row['Access']; echo "<a href=?page=UserCP>You have successfully logged in click here to access User CP.</a>"; } } } ?> </body> </html>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
0
#4 33 Days Ago
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
![]() |
Similar Threads
- Session not persisting (PHP)
- Php Sessions (PHP)
- PHP sessions/$_POST problem. Too Stressed today! (PHP)
- PHP Sessions on IIS (PHP)
Other Threads in the PHP Forum
- Previous Thread: Retrieve all $_POST values...
- Next Thread: Warning: mysql_connect()
| Thread Tools | Search this Thread |
.htaccess ajax apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code cron curl database date directory display download duplicates dynamic echo email emptydisplayvalue error errors execute explodefunction file files folder form forms function functions google href htaccess html image include insert integration ip java javasciptvalidation javascript joomla keywords limit link login loop mail menu mlm mod_rewrite multiple mysql oop oracle paypal pdf php problem query radio random recursion recursive regex remote script search server sessions shot sms soap source space sql subscription syntax system table tutorial update upload url validation validator variable video web xml youtube






