PHP Redirecting specific users?

Discussion in 'Web Design and Development' started by 76ShovelHead, Apr 17, 2012.

  1. 76ShovelHead, Apr 17, 2012
    Last edited: Apr 17, 2012

    76ShovelHead macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #1
    So I though I had it, after days of not finding what I wanted on google, I wrote a if else line of code for my php index file to redirect the user

    if $_SESSION['username'] == "username1";
    header("location: page.php")
    else
    echo "Invalid Login";

    However I wasn't out of the woods yet. I learned (I am new to PHP, only been using it for 4 days now) that you can only assign one header since the header is completed before all else.

    So then I read a line of code that looked like this, here's my implementation:

    header ("locaton: page.php?id=" . $_SESSION['ID']);

    So basically, it will route the user with whatever user ID to its corresponding page. So if I am user with ID #1 it will redirect me to the page on my server named page.php?id=1.

    Now when signing into my website it shows the browser trying to access page.php?id=1 . So I know its working since the line of code in my PHP file is getting the user ID and putting it into the URL, but the problem I'm facing is that my web server for some reason doesn't respond to .php files with that gibberish at the end.

    .php loads fine, but .php?id=1 will return an error "not found on server"

    So before I leave you asking some questions about my setup:

    Yes, I have renamed my file user.php to user.php?id=1 and the server didn't find it.

    Then I changed the extension from .php to .php?id=1 and still no avail.

    I am using the latest version of XAMPP with phpmyadmin preinstalled since OS X Lion built in web server doesn't start and I can't get it to no matter all the workarounds on the internet.

    If I were to get rid of the header and just make it echo, it would, so I know my database is working fine.

    I was using MAMP but it was so bloated? it kept freezing (application, not my mac) and the database wasn't working so I switched to xampp.

    So with that all said, anyone know why my server isn't finding my .php?id=1 page?

    Thanks for the help!

    EDIT

    Let me specify that I created an encrypted MD5 Login system with the XAMPP and phpmyadmin database creating a new database and table inside that holds the usernames, ids, and passwords. I am doing this instead of using a simple header location for all users because each user will have their private page or profile instead of a generic users page.
     
  2. Cerebrus' Maw, Apr 17, 2012
    Last edited: Apr 17, 2012

    Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #2
    Your extension should always only be .php

    A URL that contains

    www.awebsite.com/index.php?something=1&another=2

    still goes index.php. The section after the "?" represent variables that can be accessed via the $_GET array or by the $_REQUEST array, to make it possible to pass information from page to page

    Taking the above example.

    PHP:

    echo $_GET['something'];

    would simply print "1" as the variable "something" has the value "1" assigned from it in the URL.


    So, if we use the scenario you gave above in your post, your user.php file (and that is all it should be named :) ) would look like this when accessing this url: www.yoursite.com/user.php?id=1

    PHP:

    if ($_GET['id'] ==='1') {
       
    // do some stuff
    } else {
       
    //do other stuff
    }
    Most important thing to correct from your post. Your file MUST end with .php. That is the page that your server will load. Anything after the "?" is available to that page. Otherwise you would need sooooo many copies of the same page just for different users.

    PS: This is just an example to highlight your issue. Hardcoding stuff like this has scalability, maintainability and security issues. But as you are just exploring the language, you should be fine.

    Good luck!
     
  3. 76ShovelHead thread starter macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #3
    Thank you so much.

    I still haven't figured this out completely, but this changed how I was thinking.

    What I have grasped is that I should be using only 1 page for all my users because as you said it would be horrific to have to have a new page for EVERY user, so that one page is basically a template. But inside the divs of this template I can use IF ELSE statements to what users see what.

    Am I on the right track here? I know I won't be a good PHP coder overnight but I hope I'm catching on to the mind of one lol.

    Since my way of thinking has changed, the question is not how to redirect users to their corresponding pages but changing content on the pages according to which user is viewing it.

    So I'll start with something different, each user will have a profile picture I assign. Basically, it's the logo of the clients business in which I designed a site for.
    PHP:
    <? 
            if (
    $_SESSION['username'] === "newuser1") {
                
    $myImg 'media/logo.png';
            }else{
                echo 
    ""
            }
            
    ?>
    I left the echo quotes blank because I don't want users who aren't newuser1 to see the error message. Instead, I'd start another
    PHP:
    $_SESSION
    to display newuser2's logo in which I've designed.

    Am I on the right track here?

    BTW the logo isn't showing in the div lol
     
  4. 76ShovelHead thread starter macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #4
    UPDATE!

    I figured out how to make the image for newuser1 to show,

    PHP:
    <?
            
            
    $img"<img src='media/logo.png' width='100' height='100' alt='logo'>"
            
            if (
    $_SESSION['ID'] === "1") {
                echo 
    $img;
            }else{
                echo 
    "Logo"
            }
            
    ?>
    By defining $img and then echoing it if statement true I was able to make it load.
     
  5. Cerebrus' Maw, Apr 17, 2012
    Last edited: Apr 17, 2012

    Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #5
    An early issue I can see with that is what are you going to do say a year down the road and you have different users each with different logos?

    You would need differnt if/else statments for each user.

    An easier way would be to maintain each user logo within a folder designated with that username.

    EG for me, my image path could be:
    Code:
    <img src="/images/maw/logo.png" />
    
    and yours would be
    Code:
    <img src="/images/kenny/logo.png" />
    
    So rather than using arbitrary $_SESSION['ID'] (1, 2, 3, 4 etc etc)
    You could use
    PHP:
    $_SESSION['user'];
    where $_SESSION['user'] would be set to the login for your client.

    So in the end, it would look like:

    PHP:

    //DO a test to see if the session for user has been set
    if (isset($_SESSION['user'])) {
       
    //User is set, so dynamically make the file path show their logo
       
    echo '<img src="/images/'.$_SESSION['user'].'/logo.png" />';

       
    //if I was logged in, this would echo:
       //<img src="/images/maw/logo.png" />
    } else {
       
    //User is not defined, show something generic
       
    echo '<img src="/images/default.png" />';
    }

    This is just an assumption I am making from interpreting your code. If you are happy with what you got, by all means go for it.
     
  6. 76ShovelHead, Apr 17, 2012
    Last edited: Apr 17, 2012

    76ShovelHead thread starter macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #6
    :)

    Your way's better, and so I implemented it to my site, with few modifications.

    I like to make the code easier to comprehend, so:

    PHP:
    <?
         
    $img'<img src="media/'.$_SESSION['username'].'/logo.png"/>'
         
    $img0'<img src="media/logo.png"/>';

         if (isset(
    $_SESSION['username'])) {
              echo 
    $img;
         }else{ 
          echo 
    $img0
         } 
    ?>
    Basically, I created two terms (you already know this lol but I feel like explaining myself, who knows maybe some lost soul will fall into my footsteps and this forum post will help them out) So anyways those two terms being $img and $img0 are defined at the top then put into the echo. That way the server can read those top two lines of code and then put the if statement together faster.

    Now for some reason if I change the folder name forcing the else { echo } the generic logo defined as $img0 doesn't showing up. So i took it out and put your's in and still nothing. I'm investigating it now, perhaps its got to do with the number 0 in the term as well as the folder name being just 0.

    So anyways, I like that I won't have to touch the file every time I add users, all I'll have to do is create a new folder named after their username and create a logo.
     
  7. Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #7
    Are you sure you are in the else part of the statement? After all, if you have logged in on your browser, you might not have logged out.

    To make sure you are in the else part you can simply put an
    PHP:
    if (isset($_SESSION['user'])) {
       echo 
    'User SET'
    } else {
       echo 
    'User NOT SET';
    }
    User NOT SET should be printed to the screen. If it is not, then your $_SESSION['user'] is being set so you are not reaching the else part.

    A 'cheat' would be to simply invert your test as such:

    PHP:
    if ( ! isset($_SESSION['user'])) {
       echo 
    'User SET'
    } else {
       echo 
    'User NOT SET';
    }
    Notice the "!". This basically means "if (this is NOT true) then do blah, else..." Remember to take that "!" out once you are done testing.

    A little piece of advice. Get into the habit of good naming conventions for your variables. $img and $img0 seem perfectly good choices when you have 10-20 lines of code, but when you reach a couple of thousand You will get to line 1769 and wonder:

    "What the ******* does $img0 mean???"

    Instead perhaps go for $user_image for your clients and $guest_image for your non clients.
     
  8. 76ShovelHead thread starter macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #8
    I have been in that place before lol, good idea. I changed the name of my variables to $user_img and $user_undefined_img.

    I have also realized that the reason I am not seeing a default Logo for new users is because the code is looking for the session user, then looking for a folder named after that user, and then the logo.png inside of that. The way this area of my site is designed, there is a login page that redirects the user to this index (I decided to make users.php and index.php one page that is just called index.php now). So if you just tried to visit my index.php in my site directory you'd be redirected to my home page which just holds my portfolio and basic information. Since my site is designed like this, displaying a default logo for visitors wouldn't work, because visitors would just be redirected to my home page. Sorry for not realizing this sooner, I haven't had coffee in a couple days lol.

    So basically, I took it out of context thinking that if I had created a new user in my database, They'd be able to login and see a default logo If i was not able to go and create a site folder for them containing their logo. Instead to achieve this, I'd have to write an else statement that would use a default logo if the user's path didn't exist.

    Perhaps the default picture is part of the template and the users picture is on top of that for a site like Facebook? Then again with an online service like that you obviously have a folder on their server that is made when you sign up. So if I ever decided to create self registration page (Which I don't believe is necessary for my use, since it should only be accessible to my clients) I could probably include the creation of a folder with a default image inside of that to go as the default logo until I find the time to reduce the size of the current one to 100x100 px. I'm now looking into another if else statement that could assign a default logo.

    Maybe:
    PHP:
    <?
    $user_img'<img src="billing/'.$_SESSION['username'].'/logo.png"/>';
    $user_undefined_img'<img*src="billing/default/logo.png"*/>';

    if (isset(
    $_SESSION['username'])) 
                echo 
    $user_img;
            else
                echo 
    "";

    if (! isset(
    $user_img))
          echo 
    $user_undefined_img;
     else 
          echo 
    "ERROR";
    ?>
     
  9. 76ShovelHead, Apr 19, 2012
    Last edited: Apr 19, 2012

    76ShovelHead thread starter macrumors 6502a

    76ShovelHead

    Joined:
    May 30, 2010
    Location:
    Florida
    #9
    UPDATE!! :cool:

    I needed a way to display a default logo for new users whom I haven't created a folder and shrunken version of the logo I've made for them. I tried to do it by creating an if else statement that didn't work quite well. So I googled for a php statement that would serve my purpose and I discovered this handy

    PHP:
    if (file_exists($variable));
         echo 
    $variable
    else
         echo 
    $variable2

    So now its working.

    My next feat is grabbing the comments row in my ACCOUNTS table of my database and putting it into the body of my text. That way I can update the text for specific users.

    I've already created a dynamic welcome statement by playing around with php:

    PHP:
    <? echo "Welcome, "$_SESSION['username']. "."?>
    This if fun when it works out! :roll eyes:

    It's been a long, long night trying to figure out how to run a second mysql_query for the comments row of my ACCOUNTS table. I wrote a php code to display the comments row on my site, but it returned some "index #5" and not the comments I was aiming for. Any ideas?

    AND YET ANOTHER UPDATE!! :D :D :D

    I GOT IT WORKING! Thanks to an FAQ at some other PHP site.

    What did it? While I did edit it by changing the mysql query to fit my needs, the rest is what they did which was setup an array and then call for that array. I filled in my comments and variables.

    PHP:
    <? 
    $usercomments mysql_query("SELECT comments FROM accounts WHERE username='{$_SESSION['username']}'");
    $array mysql_fetch_array($usercomments);
        echo 
    $array['comments'];
            
    ?>
    I don't feel like a real Web Developer because I'm finding myself calling for such help... I hope I get better at solving this stuff. I've only been studying for a week or two now.

    I say web developer because this beyond designing, right?
     

Share This Page