无法使用angular6

时间:2018-09-07 10:57:49

标签: php angular angular6

我正在使用php作为后端在angular上的项目上工作,但是当尝试从我的角度开始与我的php会话无法正常工作时,目的是当用户注册或登录时,它将占用用户到http://localhost:4200/dashboard,并且如果用户应该刷新页面,Angular会检查php以检查他是否有会话,如果他有会话,它将使用户停留在该http://localhost:4200/dashboard中,否则会重定向他。这是我的data.service.ts

代码
        import { Injectable } from '@angular/core';
        import { HttpClient } from '@angular/common/http';
        import { Router } from '@angular/router';
        import { map } from 'rxjs/operators';
        import { Observable } from 'rxjs';

        @Injectable({
            providedIn: 'root'
        })
        export class DataService {
            theurl: string='http://127.0.0.1/testing/php/';
            public isLoggin = false;

            constructor(private http: HttpClient, public route: Router) {}
            setLoggedInStatus(value: boolean){
                this.isLoggin= value
            }
            get isLoggedIn(){
                return this.isLoggedIn
            }

            checkLogin(): Observable<cklogin>{ 
                return this.http.get<cklogin>(this.theurl+'check_session.php');
            }
        }

        interface cklogin{
            status: boolean;
        }

对于auth Guard,那就是auth.guard.ts

        import { Injectable } from '@angular/core';
        import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
        import { Observable } from 'rxjs';
        import { DataService } from './service/data.service';
        import { HttpClient } from '@angular/common/http';
        import { Router } from '@angular/router';
        import { map } from 'rxjs/operators';

        @Injectable({
            providedIn: 'root'
        })
        export class AuthGuard implements CanActivate {

            constructor(private serv: DataService, private route: Router, public http: HttpClient){}

            canActivate(
                next: ActivatedRouteSnapshot,
                state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
                if(this.serv.isLoggin){
                    return true;
                }
                return this.serv.checkLogin().pipe(map(res=> {
                    return res.status;
                })); 
            }
        }

        interface cklogin{
            status: boolean;
        }

对于register.component.ts

        import { Component, OnInit } from '@angular/core';
        import { DataService } from '../service/data.service';
        import { HttpClient } from '@angular/common/http';
        import { Register } from './register';
        import { Router } from '@angular/router';

        @Component({
            selector: 'app-register',
            templateUrl: './register.component.html',
            styleUrls: ['./register.component.css']
        })
        export class RegisterComponent implements OnInit {
            public submitted = false;
            public setputt: any = '';
            public checham: boolean = false;
            public hasError = true;

            constructor(
                private http: HttpClient, 
                private dataservice: DataService, 
                private route: Router
            ) { }

            model = new Register('','','', 0);
            ngOnInit() { }

            onSubmit(...event: Register[]) {
                this.submitted = true;
                this.http.post<eloike>(this.dataservice.theurl+'signup.php', event[0]).subscribe(
                    data => {
                        if(data.success){
                            this.checham = true;
                            this.hasError = false;
                            this.dataservice.isLoggin = true; 
                            this.route.navigate(['dashboard']);
                            this.setputt = data.message;
                        }
                        else{
                            this.submitted = false;
                            this.setputt = data.message;
                        }
                    },
                    error => {
                        this.submitted = false;
                        console.log("Error", error);
                    }
                );;
            }
        }

        interface eloike{
            'success': 'boolean',
            'message': 'string'
        }

用于signup.php

        <?php
            session_start();
            require_once 'conn.php';
            $info = json_decode(file_get_contents('php://input'));
            if(count($info)>0){
                $username = mysqli_real_escape_string($conn, $info->username);
                $password = mysqli_real_escape_string($conn, $info->password);
                $email = mysqli_real_escape_string($conn, $info->email);
                $phoneNumber = mysqli_real_escape_string($conn, $info->phoneNumber);

                $query = mysqli_query($conn, "INSERT INTO users (username, email, `password`, phoneNumber) VALUES ('".$username."', '".$email."', '".$password."', '".$phoneNumber."')");
                if($query){
                    $_SESSION['checklogin'] = $username;
                    $msg = '
                    {
                        "success": true, 
                        "message": "Data Added Successfully"
                    }';
                }
                else{
                    $msg = '
                    {
                        "success": false, 
                        "message": "Data not added Successfully"
                    }';
                }       

            }
            echo $msg;
        ?>

对于check_session.php

        <?php
            require_once 'conn.php';

            if(isset($_SESSION['checklogin'])){
                echo '{"status": true}';
            }
            else{
                echo '{"status": false}';
            }
        ?>

对于conn.php

        <?php
            error_reporting( ~E_DEPRECATED & ~E_NOTICE );

            ob_start();
            session_start();
            header("Access-Control-Allow-Origin: *");
            header("Content-Type: application/json; charset=UTF-8");
            header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
            $conn = mysqli_connect('localhost', 'root', '', 'angular');
        ?>

注册或登录后刷新http://localhost:4200/dashborad时,它带我到http://localhost:4200。我不知道我在哪里弄错了,或者为什么会议没有开始

0 个答案:

没有答案