Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Problem Setting Sessions

    Hey guys am having trouble setting up my sessions. I want to set it up in a way that if there is no session set and a user try to enter the index page he should be automatically redirected to the login page. this is what iv tried but it doesn't seem to work.


    Script for creating table "User"
    PHP Code:
    <?php
     $con
    =mysqli_connect("localhost","root","","User_db");
     
    // Check connection
     
    if (mysqli_connect_errno())
       {
       echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
       }

     
    // Create table
     
    $sql="CREATE TABLE Users (ID int (6) auto_increment, Firstname CHAR(30), Lastname CHAR(30),Username VARCHAR (16), Email VARCHAR(40), Password VARCHAR(40), Conf_Password VARCHAR(40), Location VARCHAR(280), PRIMARY KEY(ID)  )";
     

     
    // Execute query
     
    if (mysqli_query($con,$sql))
      {
      echo 
    "Table Users created successfully";
      }
    else
      {
      echo 
    "Error creating table: " .mysqli_error();
      }
     
    ?>
    Login Form
    PHP Code:
    <table align="center">
    <
    form method="post" action="LoggingIn.php" border="0" cellpadding="10"
    cellspacing="5">


    </
    tr><tr><td>Username:</td><td><input type="text" name="Username" /></td>
    </
    tr><tr><td>Password:</td><td><input type="password" name="Password" /></td>

    </
    tr><tr><td colspan="2" align="center">
    <
    input type="submit" value="Login" id="Login"/></td>
    </
    tr></form></table
    LogginIn.php

    PHP Code:
    <?php


    // Connect to server and select databse.
     
    mysql_connect("localhost""root""")or die("cannot connect"); 
     
    mysql_select_db("User_db")or die("cannot select DB");



    // username and password sent from form 
     
    $Username=$_POST['Username']; 
     
    $Password=$_POST['Password']; 



    // To protect MySQL injection (more detail about MySQL injection)
     
    $Username stripslashes($Username);
     
    $Password stripslashes($Password);
     
    $Username mysql_real_escape_string($Username);
     
    $Password mysql_real_escape_string($Password);

    $sql="SELECT * FROM Users WHERE Username='$Username' and Password='$Password'";
     
    $result=mysql_query($sql);



    // Mysql_num_row is counting table row
     
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row

    if($count==1){

    // Register $Username, $Password and redirect to file "Index.php"
     
    session_start();
     
    $_SESSION['Username']='Username';
     
    $_SESSION['Password']='Password'
     
    header("Location:Index.php");
     }
     else {
     echo 
    header("Location:Wrong_Loggin.php");;
     }
     
    ?>
    Index.php

    PHP Code:
    <?php
    // Inialize session
     
    session_start();


    // Check, if user is already login
     
    if (isset($_SESSION['Username'])) {
     
    header('Location:Login.php');
     }
     
     
    ?>

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    138
    Thanks
    3
    Thanked 19 Times in 19 Posts
    I havent run your script, just looking at it though

    PHP Code:
     $_SESSION['Username']='Username'
     
    $_SESSION['Password']='Password'
    These are strings and not variables. Meaning that the session variable user is 'Username'

    Which is fine if you are just testing, and entering the login Username for testing, but that would require clarification from you.. Also there is no need to store the users password in a session. Once you have checked they are who they are and assigned the username variable you know that they have passed login credential checking.

    Is there an error you are getting specifically??


  •  

    Posting Permissions

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