Results 1 to 4 of 4

Thread: Php session won't resume data

  1. #1
    Join Date
    Jun 2014

    Php session won't resume data

    Following are the two separate pages I have started session in login page and try to save data in session variable and then I printed the data to confirm that the variable holds the data. after that I have used header function to redirect it. when control goes to profile page the st


    include "dbconnect.php";


    $name = mysql_real_escape_string($_POST["Email"]);

    $userpwd = mysql_real_escape_string($_POST["Password"]);

    $str= "SELECT * FROM books WHERE Email ='".$name."'"."AND Password = '".$userpwd."'";

    $result=mysql_query($str)or die(mysql_error());

    if($row = mysql_fetch_array($result))









    echo $_SESSION["0"]." ".$_SESSION["1"]." ".$_SESSION["2"]." ".$_SESSION["3"]." ".$_SESSION["4"]." ".$_SESSION["5"]." ".$_SESSION["6"]." ".$_SESSION["7"];




    echo "Invaid user name or password";



    echo "Fields can't be Null";


    <h1> Profile</h1>

    <table id="csstable">

    <?php if(!isset($_SESSION["0"])){echo"not set";} ?>

    <tr><td>Name</td><td><?php echo "".$_SESSION["5"]; ?></td></tr>



    <?php echo $_SERVER['SCRIPT_FILENAME']; echo foot(); ?>

  2. #2
    Join Date
    Jul 2013
    Voorheesville NY USA
    PS - If you're posting here you should be using:

    ini_set('display_errors', '1');

    at the top of ALL php code while you develop it!

  3. #3
    Well, lemme run down what I'm seeing wrong here.

    1) you should never != a value that might not be set, that will fill your error log. The functions isset() exist to do that.

    2) This is 2014, not 2004 -- you shouldn't be using mysql_ functions, much less blindly dumping variables into queries -- hence the giant red warning boxes in the manual waving you off from using them?

    3) I'd suggest making your mysql fieldnames all lower case, since backup/restore functions can mangle case.

    4) I'd REALLY advise you NOT 'SELECT *' on a database that has a password in it. You should never run anything that retrieves the password and stores it permanently. Passwords are best sent monodirectional towards the database.

    5) I'd use an associative array instead of a numeric one.

    6) avoid string additions in echo, they're slower and force an usual execution order. Comma delimits would likely be far more useful.

    7) automate iterating the returned fields. That too should be simpler.

    8) I suggest regenerating the session ID on every access, makes MITM attacks harder. (though still not impossible, it just narrows the window)

    9) use a prefix on your session values just to reduce the odds of namespace collisions... or even better, just pass the entire row to $_SESSION.

    10) rather than wasting time on handshakes back and forth for nothing, instead of doing a redirect (a goofy method that I'm assuming some rubbish tutorial or book is out there telling people to do) just load the page to be run instead of

    11) there is rarely a reason to session_write_close.

    12) I assume your foot() routine is the page footer, if so why are you wasting memory passing a string instead of having it echo values? A more verbose name like say template_header and template_footer might be a bit more intelligible.

    13) Really might help if you encoded the password for storage AND removed it from memory as soon as you use it.

    Something more like:
    // we'll assume the above makes a $db variable that's a connected PDO object
    if (isset($_POST['Email']) && isset($_POST['Password']) {
    	$statement = $db->prepare('
    		SELECT * FROM books
    		WHERE Email = :email
    		AND Password = :password
    		':email' => $_POST['Email'],
    		':password' => hash('sha256', $_POST['Password'])
    	$_POST['Password'] = ''; // destroy it to increase security once we've used it!
    	if ($_SESSION['booksUser'] = $statement->fetch(PDO::FETCH_ASSOC)) {
    		for ($_SESSION['booksUser'] as $key => $data) {
    			echo $key, ' = ', $data, '</br />';
    	} else echo '<p>Invalid user name or password</p>';
    } else echo '<p>You must enter a user name or password</p>';
    echo '
    if (!isset($_SESSION['booksUser'])){
    	echo 'Guest';
    } else echo '
    	<table id="csstable">
    			<th scope="row">Name</th>
    			<td>', $_SESSION['booksUser']['Name'], '</td>
    THOUGH as sessions are stored in a database too, I probably would avoid making duplicates of existing data on the disk and only store the user ID (which I assume is index 0) in the session, and pull the user info as needed from the main database when you're actually going to use it for something.

    Also why when I'm doing this sort of thing my user database typically only has ID, username, and password in it, as those are the most frequently accessed values. I then have a userInfo table that has userID, field and data fields in it, allowing custom information about the user (the rarely accessed stuff) to be dynamic -- and a permissions table that can be accessed via a singleton. (with the setter being a database read, so the permissions are 'read only' via getters).

    Though some folks say I'm overly paranoid about security -- in a 'insecure by design' language, I say there's no such thing as being "too paranoid".

  4. #4
    Join Date
    May 2014
    deathshadow pointed out good practises to use on login and good ethics on php coding.
    one thing I dont suggest is storing actual user table id to identify the user after logged in.what I suggest is create a new field to store unique session id for every login (something like random code),
    for every successful login generate a random code store in the session and use it to identify the logged user.I think it should be much secure.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10899 seconds
  • Memory Usage 2,870KB
  • Queries Executed 14 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates