• Thomas Varghese

    (@thomasv1502)


    Hello all, I am trying to have a login page inside my home page.I have put two text box for username and password on my home page.I am building a church website so if the user logs in he can see his own additional details.The login parts is working fine.The problem is when the user logs in i start a separate session for each user using the $_SESSION php variable.I also have a logout label on this same page.And i am destroying the session when the user clicks the logout label but the problem is even without clicking on logout if i refresh the page the user automatically gets logout cause the session_destroy is getting executed. I tried to keep the logout part into some other file.I did that and i created a simple php page called logout.php while clicking on logout i am redirected to this page but when i try to redirect back to the home page then its showing error that call to undefined function wp_redirect. Since i am new to wordpress can anyone suggest me what can be done.Thank you in advance for all the help and advice.I have added the code in header.php which is as follows

    <?php
    /**
     * The Header for our theme.
     *
     * Displays all of the <head> section and everything up till <div id="main">
     *
     * @package WordPress
     * @subpackage Twenty_Elevena
     * @since Twenty Eleven 1.0
     */
    ?><!DOCTYPE html>
    <!--[if IE 6]>
    <html id="ie6" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 7]>
    <html id="ie7" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 8]>
    <html id="ie8" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
    <html <?php language_attributes(); ?>>
    <!--<![endif]-->
    
    <head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width" />
    <title><?php
    	/*
    	 * Print the <title> tag based on what is being viewed.
    	 */
    	global $page, $paged;
    
    	wp_title( '|', true, 'right' );
    
    	// Add the blog name.
    	bloginfo( 'name' );
    
    	// Add the blog description for the home/front page.
    	$site_description = get_bloginfo( 'description', 'display' );
    	if ( $site_description && (is_home() || is_front_page() ) )
    		echo " | $site_description";
    
    	// Add a page number if necessary:
    	if ( $paged >= 2 || $page >= 2 )
    		echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );
    
    	?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php echo bloginfo('stylesheet_directory');?>/css/style.css"/>
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.7.1.min.js"></script>
    <!--[if lt IE 9]>
    <script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script>
    <![endif]-->
    <?php
    	/* We add some JavaScript to pages with the comment form
    	 * to support sites with threaded comments (when in use).
    	 */
    	if ( is_singular() && get_option( 'thread_comments' ) )
    		wp_enqueue_script( 'comment-reply' );
    
    	/* Always have wp_head() just before the closing </head>
    	 * tag of your theme, or you will break many plugins, which
    	 * generally use this hook to add elements to <head> such
    	 * as styles, scripts, and meta tags.
    	 */
    	wp_head();
    ?>
    
    <style>
    #statusbar{
    	clear: both;
    	width: 100%;
    	color:#FFF;
    	text-align: left;
    	padding: 4px 0;
    	background-color:#999;
    	border-top: 5px solid #dadada;
    }
    </style>
    
    </head>
    
    <body>
    <div id="templatemo_container">
    <div id="templatemo_header">
            <div id="out_mission_section">
                <p>
                Lorem ipsum dolor sit amet, consec tetur adipi scing elit. Nullam sollici tudin libero. Maece nas posuere congue massa. <a>/subpage.html">more...</a>
                </p>
            </div>   
    
            <div id="daily_bible_verse_section">
            	<p> Do to others as you would have them do to you. </p>
            	<div id="bible_verse">Luke 6:31</div>
            </div>
             </div>
    
              <div id="templatemo_menu">
    
                 <?php wp_nav_menu(); ?>
    
                  <div id="statusbar">
                <table>
                <tr>
                <td>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="aform" target="_top" >
                Login Id :<input type="text" name="uname" id="uname" value="" />
                Password :<input type="password" name="pass" id="pass" value="" />
                <input type="submit" name="login" value="login" />
                <input type="reset" name="login" value="Reset" />
                </form>
                </td>
                <td width="100px" align="right">
                <label>Welcome : </label></td>
                <td width="150px"> <?php
    			session_start();
    			 if(isset($_SESSION['uname']))
    			 {
    			 	$uname=$_SESSION['uname'];
    			  	echo $uname;
                 }
                 else
                 {
                    echo "Guest";
                 }
                   ?>  
    
                </td>
                <td width="100px" align="right">
              <!--  <form id="frmlogin" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >-->
                <a>" onClick="<?php session_destroy(); ?>">Logout</a>
                <!--</form>-->
                </td></tr>
                </table>
    
                </div>
                 <?php global $wpdb;
    			if($_POST['uname'] != "" && $_POST['pass'] != "")
    			{
    				echo get_stylesheet_directory_uri();
    				//echo $_POST['uname'];
    				//echo $_POST['pass'];
    				$res=$wpdb->get_results('select * from login where uname="'.$_POST['uname'].'" and pass="'.$_POST['pass'].'"');
    
    				if($wpdb->num_rows)
    				{
    					//echo "uname".$_POST['uname'];
    		  			session_start();
    					$_SESSION['Id']=session_id();
    					$_SESSION['uname']=$_POST['uname'];
    		    		//echo $_SESSION['uname'];
    					//$flag=true;
    					//wp_redirect( home_url() ); exit;
    				}
    			} ?>
        </div>
    
    And this is the code of my logout.php page
    
    <?php
    	  session_start();
    	  if(isset($_SESSION['uname']))
    	  {
    		session_destroy();
    		add_filter( 'template_redirect', 'tccl_redirect' );
    		function tccl_redirect()
    		{
    			wp_redirect( home_url() );exit;
    		}
    
    	  }
    
    ?>

    [Please post code or markup between backticks or use the code button. Or better still – use a pastebin. Your posted code may now have been permanently damaged/corrupted by the forum’s parser.]

Viewing 4 replies - 1 through 4 (of 4 total)
  • phillbooth

    (@phillbooth)

    Try using a WP function to login

    $creds = array();
    $creds['user_login'] = $yourusername;
    $creds['user_password'] = $yourpassword;
    $creds['remember'] = true;
    
    wp_signon( $credentials, false );

    This can be in the header

    http://codex.wordpress.org/Function_Reference/wp_signon

    then

    if ( is_user_logged_in() ) {
    header('Location: http://example.com/');
    }
    Thread Starter Thomas Varghese

    (@thomasv1502)

    I am really thankful to you for the reply.I would also like to know one more thing.What if the user clicks on the logout link,how do i redirect him to the home page and can i maintain session of the logged in user using this function.Sorry for so many silly doubts.

    Thread Starter Thomas Varghese

    (@thomasv1502)

    I am really thankful to you for the reply.I would also like to know one more thing.What if the user clicks on the logout link,how do i redirect him to the home page and can i maintain session of the logged in user using this function.Sorry for so many silly doubts.

    Thread Starter Thomas Varghese

    (@thomasv1502)

    I am really thankful to you for the reply.I would also like to know one more thing.What if the user clicks on the logout link,how do i redirect him to the home page and can i maintain session of the logged in user using this function.Sorry for so many silly doubts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to redirect from a non wordpress page to wordpress page’ is closed to new replies.